limit method

String limit(
  1. int maxLength, {
  2. String ellipsis = '...',
})

Limits this string to maxLength and appends ellipsis when truncated.

Implementation

String limit(int maxLength, {String ellipsis = '...'}) {
  if (maxLength < 0) {
    throw RangeError.value(maxLength, 'maxLength', 'must not be negative');
  }
  if (this == null || this!.length <= maxLength) return this ?? '';
  if (maxLength <= ellipsis.length) {
    return ellipsis.substring(0, maxLength);
  }
  return '${this!.substring(0, maxLength - ellipsis.length)}$ellipsis';
}