fromJson static method

CooldownSchema? fromJson(
  1. dynamic value
)

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

Implementation

// ignore: prefer_constructors_over_static_methods
static CooldownSchema? 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'total_seconds'),
          'Required key "CooldownSchema[total_seconds]" is missing from JSON.');
      assert(json[r'total_seconds'] != null,
          'Required key "CooldownSchema[total_seconds]" has a null value in JSON.');
      assert(json.containsKey(r'remaining_seconds'),
          'Required key "CooldownSchema[remaining_seconds]" is missing from JSON.');
      assert(json[r'remaining_seconds'] != null,
          'Required key "CooldownSchema[remaining_seconds]" has a null value in JSON.');
      assert(json.containsKey(r'started_at'),
          'Required key "CooldownSchema[started_at]" is missing from JSON.');
      assert(json[r'started_at'] != null,
          'Required key "CooldownSchema[started_at]" has a null value in JSON.');
      assert(json.containsKey(r'expiration'),
          'Required key "CooldownSchema[expiration]" is missing from JSON.');
      assert(json[r'expiration'] != null,
          'Required key "CooldownSchema[expiration]" has a null value in JSON.');
      assert(json.containsKey(r'reason'),
          'Required key "CooldownSchema[reason]" is missing from JSON.');
      assert(json[r'reason'] != null,
          'Required key "CooldownSchema[reason]" has a null value in JSON.');
      return true;
    }());

    return CooldownSchema(
      totalSeconds: mapValueOfType<int>(json, r'total_seconds')!,
      remainingSeconds: mapValueOfType<int>(json, r'remaining_seconds')!,
      startedAt: mapDateTime(json, r'started_at', r'')!,
      expiration: mapDateTime(json, r'expiration', r'')!,
      reason: ActionType.fromJson(json[r'reason'])!,
    );
  }
  return null;
}