truncate function Null safety

String truncate(
  1. String string,
  2. int length,
  3. [String? omission]

Truncates string if it's longer than the given maximum string length. The last characters of the truncated string are replaced with the omission string which defaults to "...".


String truncate(String string, int length, [String? omission]) {
  if (string.length <= length) {
    return string;
  omission ??= '...';
  return string.substring(0, length - omission.length) + omission;