maxChars method Null safety

String? maxChars(
  1. int n
)

Trims the String to have maximum n characters.`

Example

String foo = 'esentis';
String newFoo = foo.maxChars(3); // 'esen';

Implementation

String? maxChars(int n) {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return this;
  }
  if (n <= 0) {
    return '';
  }
  if (n >= this!.length) {
    return this;
  }
  return this!.substring(0, n);
}