getBool method

bool getBool(
  1. String key
)

Implementation

bool getBool(String key){
  if (!containsKey(key)) {
    throw Exception("json.getBool($key). No key");
  }
  final value = this[key];
  if (value is bool){
    return value;
  }
  if (value is int){
    if (value == 0) return false;
    if (value == 1) return true;
    throw Exception("could not parse int $value to bool (1 or 0 only)");
  }
  if (value is double){
    if (value == 0) return false;
    if (value == 1.0) return true;
    throw Exception("could not parse double $value to bool (1 or 0 only)");
  }
  if (value is String){
    if (value.trim().toLowerCase() == 'true') return true;
    if (value.trim().toLowerCase() == 'false') return false;
    throw Exception("could not parse string $value to bool ('true' or 'false' only)");
  }

  throw Exception("could not parse value $value to bool");
}