charCount method Null safety

int? charCount(
  1. String char
)

Finds a specific's character occurence in a string

Example

String foo = 'foo';
int occ = foo.charCount('o') // returns 2

Implementation

int? charCount(String char) {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return 0;
  }
  return this!.split('').fold<int>(
      0, (previousValue, ch) => previousValue + (ch == char ? 1 : 0));
}