asBool method

bool asBool(
  1. String key, {
  2. dynamic def = false,
})

Retrieves the boolean value associated with the key. If the key does not exist or the value cannot be parsed as a boolean, returns the def value.

key The key in the map to retrieve the value from. def The default value to return if the key is not found or the value cannot be parsed. Defaults to false.

Returns: The boolean value associated with the key or def if not found or cannot be parsed.

Implementation

bool asBool(String key, {def = false}) {
  if (keys.contains(key)) {
    if (this[key] is bool) {
      return this[key];
    } else if (this[key] is String) {
      var val = this[key].toString().trim().toLowerCase();
      if (val == 'true' || val == '1' || val.isNotEmpty) {
        return true;
      } else {
        return false;
      }
    } else if (this[key] is int) {
      return (this[key] as int) > 0;
    }
  }

  return def;
}