toTitleCase method Null safety

String? toTitleCase()

Returns the word title cased.

String foo = 'Hello dear friend how you doing ?';
Sting titleCased = foo.toTitleCase() // returns 'Hello Dear Friend How You Doing'.

Implementation

String? toTitleCase() {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return this;
  }
  var words = this!.toLowerCase().split(' ');
  for (var i = 0; i < words.length; i++) {
    words[i] = words[i].substring(0, 1).toUpperCase() + words[i].substring(1);
  }

  return words.join(' ');
}