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) {
      try {
        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;
        }

        if (field is JsonObject) {
          field.value = create<DataModel>(field.genericType);
          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) {
            field.fromJSON(json[field.name]);
          }
          continue;
        }
      } catch (error) {
        final message =
            "Error while parsing field ${field.name}: ${error.toString()}";
        print(message);
        Sentry.captureMessage(message);
        Sentry.captureException(error);
      }
    }
    return;
  }
  throw Exception(
    "Data passed to DataModel.fromJSON must be a Map<String, dynamic>",
  );
}