toNum method

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.isBlank) {
    return null;
  }

  return num.tryParse(this!);
}