toNum method Null safety

num? toNum()

Converts a string to a numeric value if possible.

If conversion fails, null is returned.

Example

String foo = '4';
int fooInt = foo.toNum(); // returns 4;
String foo = '4f';
var fooNull = foo.toNum(); // returns null;

Implementation

num? toNum() {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return null;
  }
  return num.tryParse(this!);
}