limitString function Null safety

String limitString(
  1. String string,
  2. dynamic maxSize,
  3. {String ellipsis = '...'}
)

Returns the length limited string. string the string to convert, e.g. "abc123" maxSize: the maximum length, e.g. 5 ellipsis: null or a suffix which signals the cut of the string return string (if enough space or null) or the limited string, e.g. "ab..."

Implementation

String limitString(String string, maxSize, {String ellipsis = '...'}) {
  String? rc;
  maxSize ??= 80;
  if (string.length > maxSize) {
    if (maxSize < ellipsis.length + 1) {
      rc = string.substring(0, maxSize);
    } else {
      rc = string.substring(0, maxSize - ellipsis.length) + ellipsis;
    }
  }
  rc ??= string;
  return rc;
}