fromJson method
Deserializes JSON data into this model instance.
This method populates the model's fields from the provided JSON data and handles error/warning/information messages from the server response. It automatically:
- Extracts general errors, warnings, and informational messages
- Maps field-specific errors, warnings, and informations
- Deserializes all field values from the JSON map
- Assigns validation messages to the corresponding fields
Parameters:
json: The JSON data to deserialize. Can be aMap<String, dynamic>or any other type (in which case deserialization is skipped).
Example:
final user = UserModel();
user.fromJson({
'name': 'John Doe',
'age': 30,
'errors': {'email': 'Invalid email format'},
'generalErrors': ['Account verification required']
});
Implementation
@override
void fromJson(json) {
super.fromJson(json);
if (json is Map<String, dynamic>) {
if (json.containsKey("displayName") &&
json["displayName"] is String &&
json["displayName"].isNotEmpty &&
name.value.isEmpty) {
name.value = json["displayName"];
}
}
}