toBool static method

bool? toBool(
  1. dynamic v, {
  2. List<String> trues = const ['true', 'True'],
  3. List<String> falses = const ['false', 'False'],
  4. 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!');
}