toInt property
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? get toInt {
if (isBlank) {
return null;
}
//check if string is an hexadecimal
if (isHexadecimal) {
return int.tryParse(removeFirstEqual("#"), radix: 16);
}
return int.tryParse(this) ?? double.tryParse(this)?.floor();
}