squeeze method Null safety

String? squeeze(
  1. String char
)

Squeezes the string by removing repeats of a given character.

Example

String foo = 'foofoofoofoofoo';
String fooSqueezed = foo.squeeze('o'); // 'fofofofofo';

Implementation

String? squeeze(String char) {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return this;
  }
  var sb = '';
  for (var i = 0; i < this!.length; i++) {
    if (i == 0 ||
        this![i - 1] != this![i] ||
        (this![i - 1] == this![i] && this![i] != char)) {
      sb += this![i];
    }
  }
  return sb;
}