truncToLength method

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

Truncates the string to the given length. Specify ellipsis to append to he truncated string if it is shorter than length, e.g. 'Hello World'.truncToLength(5, ellipsis: '...') => 'Hello...'.

Implementation

String truncToLength(
  int length, {
  String ellipsis = '',
}) {
  final temp = (this.length > length ? substring(0, length).trim() : this);
  if (temp.length < length) return temp + ellipsis;
  return temp;
}