I have a class like this:
class UserModel {
String id = '';
String firstName = '';
String lastName = '';
String email = '';
String phoneNumber = '';
String? createdDate = '';
String? membershipType = '';
UserModel({
required this.id,
required this.firstName,
required this.lastName,
required this.email,
required this.phoneNumber,
required this.createdDate,
required this.membershipType
});
UserModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
firstName = json['firstName'];
lastName = json['lastName'];
email = json['email'];
phoneNumber = json['phoneNumber'];
createdDate = json['createdDate'];
membershipType = json['membershipType'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['firstName'] = firstName;
data['lastName'] = lastName;
data['email'] = email;
data['phoneNumber'] = phoneNumber;
data['createdDate'] = createdDate;
data['membershipType'] = membershipType;
return data;
}
}
I want to avoid using dynamic
type and I want to cleanly handle errors that may occur from malformed JSON. I also want to avoid code generation since it will mess up my build pipelines and introduce significant overhead into my project.
Is there a library that uses generics or other features to provide a better developer experience? Is there a better way to manually implement JSON serialization without dynamic
?
2
Answers
Unfortunately, it is currently not possible, except by using code generators or manually. There is an experimental proposal called Dart macros:
In the documentation, you can see how to install and test it, and the first example of a use case is this type of situation. Using the
JsonCodable
annotation allows your class to have thefromJson
andtoJson
methods generated automatically.More information at: Dart Macros Documentation
In dart language, JSON used
dynamic
because we cannot expect one data type because we have different data types such asString
,Int
,List
like that.But in this issue, we want to avoid dynamic so , we can use exact data type with nullsefty instance of
dynamic
.We can use manual serialization without
dynamic
.look at this code so I use empty value as default values. firstly we get it as string and when it’s null we can set default values. typically we cannot set data from data base because those are come from backend that’s why we use empty values as default values.