-
Notifications
You must be signed in to change notification settings - Fork 34
Default values
Ravi Teja Gudapati edited this page Jun 14, 2018
·
3 revisions
For a non-nullable field, during de-serialization, one can request that a default value be assigned to the field if its value is null. The default value is obtained from getJserDefault
method of Serializer
. This method can be overloaded to configure default values of non-nullable fields.
class User {
String name;
int age;
}
@GenSerializer(nullableFields: false)
class UserSerializer extends Serializer<User> with _$UserSerializer {
T getJserDefault<T>(String field) {
switch(field) {
case 'name': return 'Messi' as T;
case 'age': return 30 as T;
default: return null;
}
}
}
Note, how the below deserialization results in usage of default values for name
and age
fields.
UserSerializer.fromMap({}) // => Player(name: 'Messi', age: 30)