truncate method

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

Truncates the string to the specified length and adds an ellipsis if truncated.

@param length The maximum length of the returned string, not including the ellipsis. @param ellipsis The string to append if truncation occurs. Defaults to '...'. @return The truncated string with ellipsis if needed, or the original string if no truncation occurred.

Implementation

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