0

我无法对模型及其中的列表执行数据“toJson”操作。

主要型号

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())),
  };
}

子模型 列表模型链接到主模型

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,
  };
}

如何在其中添加主模型和列表模型? 如何在其中映射主模型和列表模型。

var newList = MainController.to.getExampleMain.map((value) => value.toJson());
await MainService.addExample(postBody: newList);

在此处输入图像描述

谢谢,

4

3 回答 3

1

基于模型类 json字符串

[{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}]}]

飞镖板

示例代码

 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;
              }
            }
于 2022-02-25T09:25:37.243 回答
1

您只需要在查询结束时添加 toList() 即可。

var newList = MainController.to.getExampleMain.map((value) => value.toJson()).toList();
于 2022-02-25T08:54:19.820 回答
0

您可以从 JSON 结构自动生成所需的模型,您可以使用在线生成器,例如quicktype.io,只需粘贴您的 JSON,选择 dart 语言并生成您的模型。

于 2022-02-25T08:55:15.697 回答