truncate method

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

Truncates this string to maxLength characters, appending ellipsis if truncation occurs.

'Hello World'.truncate(7) // 'Hello W…'

Implementation

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