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,
CodePudding user response:
You just need to add toList() at the end of query.
var newList = MainController.to.getExampleMain.map((value) => value.toJson()).toList();
CodePudding user response:
Basedon on model Class json string
[{name: hi, surname: joji, otherlist: [{name: kl, surname: ja}, {name: lkl, surname: lja}]}, {name: hohi, surname: jpjoji, otherlist: [{name: kl, surname: ja}, {name: lkl, surname: lja}]}]
Sample Code
class MainController {
static List<ExampleData2> to() {
List<ExampleData2> data = [];
List<Otherlist> otherlist = [];
otherlist.add(new Otherlist(surname: "ja", name: "kl"));
otherlist.add(new Otherlist(surname: "lja", name: "lkl"));
data.add(
new ExampleData2(name: "hi", surname: "joji", otherlist: otherlist));
data.add(new ExampleData2(
name: "hohi", surname: "jpjoji", otherlist: otherlist));
return data;
}
}
void main() {
// ---------------------- json
var list = MainController.to().map((e) => e.toJson()).toList();
print(list);
var d = list.map((e) => ExampleData2.fromJson(e)).toList();
print(d);
}
class ExampleData2 {
String? name;
String? surname;
List<Otherlist>? otherlist;
ExampleData2({this.name, this.surname, this.otherlist});
ExampleData2.fromJson(Map<String, dynamic> json) {
name = json['name'];
surname = json['surname'];
if (json['otherlist'] != null) {
otherlist = <Otherlist>[];
json['otherlist'].forEach((v) {
otherlist!.add(new Otherlist.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['surname'] = this.surname;
if (this.otherlist != null) {
data['otherlist'] = this.otherlist!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Otherlist {
String? name;
String? surname;
Otherlist({this.name, this.surname});
Otherlist.fromJson(Map<String, dynamic> json) {
name = json['name'];
surname = json['surname'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['surname'] = this.surname;
return data;
}
}
CodePudding user response:
You can generate required Model automatically from JSON structure, you can use online generators e.g. quicktype.io, just paste your JSON, select dart language and generate your model.