truncate method

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

Truncates this string to the specified length and adds ellipsis

maxLength Maximum length of the string ellipsis The ellipsis string to append (defaults to '...')

Example:

print('Hello World'.truncate(8)); // "Hello..."
print('Hello World'.truncate(8, ellipsis: '…')); // "Hello W…"

Implementation

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