padEnd function Null safety

String padEnd(
  1. String string,
  2. int length,
  3. [String? chars]
)

Pads string on the right side if it's shorter than length. Padding characters are truncated if they exceed length.

Implementation

String padEnd(String string, int length, [String? chars]) {
  chars ??= ' ';
  if (string.length < length) {
    final padLength = (length - string.length) ~/ chars.length;
    final padString = List.filled(padLength, chars).join();
    return string +
        padString.substring(0, padLength * chars.length) +
        padString.substring(padLength * chars.length);
  }
  return string;
}