I have a class as follows which i need to read/write to/from FireStore.
class Customer{
String fname;
String fname;
Map<String, dynamic> toJson() => {
'fname': fname,
'lname': lname
};
static Customer fromDocument(DocumentSnapshot doc){
Customer customer = Customer();
customer.fname = doc.data['fname'];
customer.lname = doc.data['lname'];
return customer;
}
}
How can I avoid writing code to serialize(toJson) /deserilize (fromDocument)? For amazon if i have to do this in java i would use Gson library to serialize and deserialize.
You can use json_serializable package, that generates code for serializing/deserializing. With it your code will look like that:
@JsonSerializable()
class Customer {
Customer({this.fname, this.lname});
final String fname;
final String lname;
Map<String, dynamic> toJson() => _$CustomerToJson(this);
static Customer fromJson(Map<String, dynamic> json) =>
_$CustomerFromJson(json);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加