toInt method

int? toInt()

Converts a string toint if possible.

If conversion fails, null is returned.

Example

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

Implementation

int? toInt() {
  if (this == null) return null;
  if (this!.isEmpty) return null;
  return int.tryParse(this!) ?? double.tryParse(this!)?.floor();
}