truncateWithEllipsis method

  1. @useResult
String truncateWithEllipsis(
  1. int? cutoff
)

Returns the string truncated to cutoff graphemes with an ellipsis appended.

Uses grapheme clusters for proper Unicode support, including emojis. Returns the original string if it's shorter than cutoff.

Implementation

@useResult
String truncateWithEllipsis(int? cutoff) {
  if (isEmpty || cutoff == null || cutoff <= 0) {
    return this;
  }

  if (characters.length <= cutoff) {
    return this;
  }

  return '${substringSafe(0, cutoff)}$ellipsis';
}