toInt method
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.isBlank) {
return null;
}
return int.tryParse(this) ?? double.tryParse(this)?.floor();
}