fromJSON method

  1. @override
void fromJSON(
  1. dynamic json
)
override

Implementation

@override
void fromJSON(dynamic json) {
  if (json is Map<String, dynamic>) {
    final Map<String, dynamic> errors =
        json.containsKey("errors") && json["errors"] is Map
            ? json["errors"]
            : {};

    final Map<String, dynamic> warnings =
        json.containsKey("warnings") && json["warnings"] is Map
            ? json["warnings"]
            : {};

    final Map<String, dynamic> informations =
        json.containsKey("informations") && json["informations"] is Map
            ? json["informations"]
            : {};

    for (final field in fields) {
      if (errors.containsKey(field.name)) {
        field.error = errors[field.name];
      }

      if (warnings.containsKey(field.name)) {
        field.warning = warnings[field.name];
      }

      if (informations.containsKey(field.name)) {
        field.information = informations[field.name];
      }

      if (field is JsonString ||
          field is JsonBoolean ||
          field is JsonNumber) {
        field.value = json[field.name];
        continue;
      }

      if (field is JsonDate) {
        if (json.containsKey(field.name) && json[field.name] != null) {
          field.value = DateTime.parse(json[field.name]);
        } else {
          field.value = null;
        }
        continue;
      }

      final ModelType type = _modelTypes[field.genericType]!;

      if (field is JsonObject) {
        field.value = type.constructor();
        if (json.containsKey(field.name) && json[field.name] != null) {
          field.fromJSON(json[field.name]);
        }
        continue;
      }

      if (field is JsonList) {
        if (json.containsKey(field.name) && json[field.name] is List) {
          final length = json[field.name].length;

          for (var i = 0; i < length; i++) {
            final instance = type.constructor();
            instance.fromJSON(json[field.name][i]);
            field.value?[i] = instance;
          }
        } else {
          field.value = [];
        }
        continue;
      }
    }
    return;
  }
  throw Exception(
    "Data passed to DataModel.fromJSON must be a Map<String, dynamic>",
  );
}