I cannot perform a data "toJson" operation of a model and the list in it.
main model
class ExampleData {
ExampleData({
this.name,
this.surname,
this.otherList,
});
String name;
String surname;
List<ExampleOtherData> otherList;
factory ExampleData.fromJson(Map<String, dynamic> json) =>
ExampleData(
name: json["name"] == null ? null : json["depoAdi"],
surname: json["surname"] == null ? null : json["aciklama"],
otherList: json["otherList"] == null
? null
: List<ExampleOtherData>.from(
json["otherList"].map((x) => ExampleOtherData.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"name": name == null ? null : name,
"surname": surname == null ? null : surname,
"otherList": otherList == null
? null
: List<dynamic>.from(otherList.map((x) => x.toJson())),
};
}
sub model list model linked to main model
class ExampleOtherData {
ExampleOtherData({
this.phone,
this.email
});
String phone;
String email;
factory ExampleOtherData.fromJson(Map<String, dynamic> json) =>
ExampleOtherData(
phone: json["phone"] == null ? null : json["phone"],
email: json["email"] == null ? null : json["email"],
);
Map<String, dynamic> toJson() => {
"phone": phone == null ? null : phone,
"email": email == null ? null : email,
};
}
How to add main model and list model in it? How can I map the main model and the list model in it.
var newList = MainController.to.getExampleMain.map((value) => value.toJson());
await MainService.addExample(postBody: newList);
Thank you,