fromJSON method

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

Deserializes the JSON data to the model.

Parameters:

  • json: The JSON data to deserialize.

Implementation

@override
void fromJSON(dynamic json) {
  if (json is Map<dynamic, dynamic>) {
    if (json.containsKey("generalErrors") && json["generalErrors"] is List) {
      generalErrors = (json["generalErrors"] as List<dynamic>)
          .map((dynamic item) => item as String)
          .toList();
    }

    if (json.containsKey("generalWarnings") &&
        json["generalWarnings"] is List) {
      generalWarnings = (json["generalWarnings"] as List<dynamic>)
          .map((dynamic item) => item as String)
          .toList();
    }

    if (json.containsKey("generalInformations") &&
        json["generalInformations"] is List) {
      generalInformations = (json["generalInformations"] as List<dynamic>)
          .map((dynamic item) => item as String)
          .toList();
    }

    if (json.containsKey("errors") && json["errors"] is Map) {
      errors = Map.fromEntries(
        (json["errors"] as Map)
            .entries
            .map((MapEntry<dynamic, dynamic> entry) {
          final key = entry.key as String;
          final value = entry.value as String?;
          return MapEntry(key, value);
        }),
      );
    }

    if (json.containsKey("warnings") && json["warnings"] is Map) {
      warnings = Map.fromEntries(
        (json["warnings"] as Map)
            .entries
            .map((MapEntry<dynamic, dynamic> entry) {
          final key = entry.key as String;
          final value = entry.value as String?;
          return MapEntry(key, value);
        }),
      );
    }

    if (json.containsKey("informations") && json["informations"] is Map) {
      informations = Map.fromEntries(
        (json["informations"] as Map)
            .entries
            .map((MapEntry<dynamic, dynamic> entry) {
          final key = entry.key as String;
          final value = entry.value as String?;
          return MapEntry(key, value);
        }),
      );
    }

    for (final field in fields) {
      if (json.containsKey(field.fieldName)) {
        field.value = json[field.fieldName];
      }

      if (errors.containsKey(field.fieldName)) {
        field.error = errors[field.fieldName];
      }

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

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