safeToInt static method
将其他类型转换为int
Implementation
static int safeToInt(dynamic source, {int def = 0}) {
try {
if (source != null) {
if (source is int) {
return source;
}
if (source is double) {
return source.truncate();
}
if (source is bool) {
return source ? 0 : 1;
}
return int.tryParse(source.toString()) ?? def;
}
} catch (e) {
//do nothing
}
return def;
}