toBool static method
bool?
toBool(
- dynamic v, {
- List<String> trues = const ['true', 'True'],
- List<String> falses = const ['false', 'False'],
- bool? defaultValue,
})
Implementation
static bool? toBool(v,
{List<String> trues = const ['true', 'True'],
List<String> falses = const ['false', 'False'],
bool? defaultValue}) {
if (v == null) return defaultValue;
if (v is bool) return v;
if (v is num) return v != 0;
if (v is String) {
if (trues.contains(v)) return true;
if (falses.contains(v)) return false;
if (defaultValue != null) return defaultValue;
throw Exception('Trying to convert a non-bool to bool!');
}
if (defaultValue != null) return defaultValue;
throw Exception('Trying to convert a non-bool to bool!');
}