boolFromJson static method

bool? boolFromJson(
  1. Map<String, dynamic> json,
  2. String attribute, {
  3. bool? defaultValue,
})

Implementation

static bool? boolFromJson(Map<String, dynamic> json, String attribute,
    {bool? defaultValue}) {
  try {
    if (json[attribute] != null) {
      if (json[attribute] is bool) {
        return json[attribute];
      } else if ((json[attribute] is String) &&
          !['0', '', 'false'].contains(json[attribute])) {
        return true;
      } else if ((json[attribute] is int) &&
          ![0, -1].contains(json[attribute])) {
        return true;
      }
      return false;
    }
    return defaultValue;
  } catch (e) {
    throw Exception('Error while parsing $attribute[$e]');
  }
}