toInteger static method
Tries to parse a value into an int. Returns null on failure.
Handles int, double, bool, and String inputs.
Implementation
static int? toInteger(dynamic value) {
if (value is int) {
return value;
}
if (value == null) {
return null;
}
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();
}
return null;
}