fromJson static method

ItemSchema? fromJson(
  1. dynamic value
)

Returns a new ItemSchema instance and imports its values from value if it's a Map, null otherwise.

Implementation

// ignore: prefer_constructors_over_static_methods
static ItemSchema? fromJson(dynamic value) {
  if (value is Map) {
    final json = value.cast<String, dynamic>();

    // Ensure that the map contains the required keys.
    // Note 1: the values aren't checked for validity beyond being non-null.
    // Note 2: this code is stripped in release mode!
    assert(() {
      assert(json.containsKey(r'name'),
          'Required key "ItemSchema[name]" is missing from JSON.');
      assert(json[r'name'] != null,
          'Required key "ItemSchema[name]" has a null value in JSON.');
      assert(json.containsKey(r'code'),
          'Required key "ItemSchema[code]" is missing from JSON.');
      assert(json[r'code'] != null,
          'Required key "ItemSchema[code]" has a null value in JSON.');
      assert(json.containsKey(r'level'),
          'Required key "ItemSchema[level]" is missing from JSON.');
      assert(json[r'level'] != null,
          'Required key "ItemSchema[level]" has a null value in JSON.');
      assert(json.containsKey(r'type'),
          'Required key "ItemSchema[type]" is missing from JSON.');
      assert(json[r'type'] != null,
          'Required key "ItemSchema[type]" has a null value in JSON.');
      assert(json.containsKey(r'subtype'),
          'Required key "ItemSchema[subtype]" is missing from JSON.');
      assert(json[r'subtype'] != null,
          'Required key "ItemSchema[subtype]" has a null value in JSON.');
      assert(json.containsKey(r'description'),
          'Required key "ItemSchema[description]" is missing from JSON.');
      assert(json[r'description'] != null,
          'Required key "ItemSchema[description]" has a null value in JSON.');
      assert(json.containsKey(r'tradeable'),
          'Required key "ItemSchema[tradeable]" is missing from JSON.');
      assert(json[r'tradeable'] != null,
          'Required key "ItemSchema[tradeable]" has a null value in JSON.');
      return true;
    }());

    return ItemSchema(
      name: mapValueOfType<String>(json, r'name')!,
      code: mapValueOfType<String>(json, r'code')!,
      level: mapValueOfType<int>(json, r'level')!,
      type: mapValueOfType<String>(json, r'type')!,
      subtype: mapValueOfType<String>(json, r'subtype')!,
      description: mapValueOfType<String>(json, r'description')!,
      conditions: ConditionSchema.listFromJson(json[r'conditions']),
      effects: SimpleEffectSchema.listFromJson(json[r'effects']),
      craft: CraftSchema.fromJson(json[r'craft']),
      tradeable: mapValueOfType<bool>(json, r'tradeable')!,
      recyclable: mapValueOfType<bool>(json, r'recyclable') ?? false,
    );
  }
  return null;
}