dynamicToBaseType static method
将动态类型转换成实际基础类型:String、int、double、num、bool,如果
- strict 如果为true,对于非基础类型将一律返回null
Implementation
static dynamic dynamicToBaseType(dynamic value, [bool? strict]) {
String type = value.runtimeType.toString();
if (type == 'String') {
dynamic v = int.tryParse(value);
if (v != null) return v;
v = double.tryParse(value);
if (v != null) return v;
v = bool.tryParse(value);
if (v != null) return v;
return value;
}
if (type == 'int') return value;
if (type == 'double') return value;
if (type == 'bool') return value;
if (type == 'num') return value;
return strict == true ? null : value;
}