toInteger static method
Tries to parse value into integer
.
null, int, double, bool, String
If none found, then defaultValue
is returned.
Implementation
static int toInteger(dynamic value, {int defaultValue = 0}) {
if (value is int) {
return value;
}
if (value == null) {
return defaultValue;
}
if (value is double) {
return value.toInt();
}
if (value is bool) {
return value ? 1 : 0;
}
if (value is String) {
return int.tryParse(value) ??
double.tryParse(value)?.toInt() ??
defaultValue;
}
return defaultValue;
}