truncate method

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

Truncates the string to maxLength characters. If the string is longer than maxLength, it is cut and ellipsis (default '...') is appended. The returned string's total length will not exceed maxLength.

Implementation

String truncate(int maxLength, {String ellipsis = '...'}) {
  if (length <= maxLength) return this;
  final cutoff = maxLength - ellipsis.length;
  if (cutoff <= 0) return ellipsis.substring(0, maxLength);
  return '${substring(0, cutoff)}$ellipsis';
}