getBool static method

bool? getBool(
  1. Map map,
  2. String key, [
  3. bool? defaultValue
])

Returns the value at key coerced to bool, or defaultValue.

Numbers are truthy when non-zero. The strings 'true'/'false'/'1'/ '0'/'yes'/'no'/'on'/'off' are recognized (case-insensitive, trimmed). Returns defaultValue on missing path or unrecognized value.

Implementation

static bool? getBool(Map map, String key, [bool? defaultValue]) {
  final v = get(map, key);
  if (v == null) return defaultValue;
  if (v is bool) return v;
  if (v is num) return v != 0;
  if (v is String) {
    switch (v.toLowerCase().trim()) {
      case 'true':
      case '1':
      case 'yes':
      case 'on':
        return true;
      case 'false':
      case '0':
      case 'no':
      case 'off':
        return false;
    }
  }
  return defaultValue;
}