fromJSON method
void
fromJSON(
- dynamic json
)
override
Implementation
@override
void fromJSON(json) {
rawJson = json;
if (json is Map<String, dynamic>) {
final Map<String, dynamic> errors =
json.containsKey(errorKey) && json[errorKey] is Map
? json[errorKey]
: {};
final Map<String, dynamic> warnings =
json.containsKey(warningKey) && json[warningKey] is Map
? json[warningKey]
: {};
final Map<String, dynamic> informations =
json.containsKey(informationKey) && json[informationKey] is Map
? json[informationKey]
: {};
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 is JsonInteger ||
field is JsonDouble) {
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]);
}
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) {
_logger.severe(
"Error while parsing field ${field.name}: ${error.toString()}");
}
}
return;
}
throw Exception(
"Data passed to DataModel.fromJSON must be a Map<String, dynamic>",
);
}