leftOf method Null safety

String? leftOf(
  1. String char
)

Returns the left side of the String starting from char

Example

 String s = 'peanutbutter';
 String foo = s.leftOf('butter'); // returns 'peanut'

Implementation

String? leftOf(String char) {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return this;
  }

  int index = this!.indexOf(char);
  return this!.substring(0, index);
}