-1

我建立的每个人都建立了一个休息 API,它从父母到孩子具有一对一的关系。一个父母可以有很多孩子。

这可能是端点 https://example.com/api/child?email=abc@gmail.com的可能链接, 我的 json 结果是

{
"data": [
    {
        "id": 1,
        "name": "chi",
        "pin": 123123,
        "parent_id": 1,
        "avatar_id": null,
        "created_at": "2020-12-22T13:27:59.000000Z",
        "updated_at": "2020-12-22T13:27:59.000000Z"
    },
    {
        "id": 2,
        "name": "chi",
        "pin": 123123,
        "parent_id": 1,
        "avatar_id": null,
        "created_at": "2020-12-22T13:30:35.000000Z",
        "updated_at": "2020-12-22T13:30:35.000000Z"
    },
    {
        "id": 5,
        "name": "jazzzz",
        "pin": 12121212,
        "parent_id": 1,
        "avatar_id": null,
        "created_at": "2020-12-22T17:00:07.000000Z",
        "updated_at": "2020-12-22T17:00:07.000000Z"
    },
    {
        "id": 6,
        "name": "jazzzz",
        "pin": 12121212,
        "parent_id": 1,
        "avatar_id": null,
        "created_at": "2020-12-22T17:00:57.000000Z",
        "updated_at": "2020-12-22T17:00:57.000000Z"
    },
    {
        "id": 7,
        "name": "jazzzz",
        "pin": 12121212,
        "parent_id": 1,
        "avatar_id": null,
        "created_at": "2020-12-22T17:01:18.000000Z",
        "updated_at": "2020-12-22T17:01:18.000000Z"
    },
    {
        "id": 8,
        "name": "chi",
        "pin": 123123,
        "parent_id": 1,
        "avatar_id": null,
        "created_at": "2020-12-23T09:05:34.000000Z",
        "updated_at": "2020-12-23T09:05:34.000000Z"
    }
]

}

我的模型文件是

 class ChildModel {
  List<Data> data;

  ChildModel({this.data});

  ChildModel.fromJson(Map<String, dynamic> json) {
    if (json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v) {
        data.add(new Data.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.data != null) {
      data['data'] = this.data.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Data {
  int id;
  String name;
  int pin;
  int parentId;
  Null avatarId;
  String createdAt;
  String updatedAt;

  Data(
      {this.id,
      this.name,
      this.pin,
      this.parentId,
      this.avatarId,
      this.createdAt,
      this.updatedAt});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    pin = json['pin'];
    parentId = json['parent_id'];
    avatarId = json['avatar_id'];
    createdAt = json['created_at'];
    updatedAt = json['updated_at'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['pin'] = this.pin;
    data['parent_id'] = this.parentId;
    data['avatar_id'] = this.avatarId;
    data['created_at'] = this.createdAt;
    data['updated_at'] = this.updatedAt;
    return data;
  }
}

我想在 listview 中显示 parent_id 为 1 的所有孩子。我不知道如何快速实现这一点。任何帮助将不胜感激。

4

2 回答 2

2

您应该首先使用 http 进行 api 调用。然后将 json 响应解析为模型类的列表。然后使用 ListView 或您喜欢的任何小部件来显示数据。

var res = await http.get(API_URL)
var json = jsonDecode(res.body)
var data = json['data'].map((e)=>ChildModel.fromJson(e)).toList()

文档中有关于如何在 ListView 中呈现数据的全面说明。

总的来说,您可以创建一个有状态的小部件来显示来自状态的数据。它应该从状态类的方法中调用fetchData()方法,如果挂载了小部件,则在加载数据后调用。虽然这不是一个好的做法,但它已经足够开始了。onCreated()setState((){});

于 2020-12-23T14:38:27.163 回答
0

我已经通过更改模型类解决了这个问题

   ChildModel childModelFromJson(String str) =>
    ChildModel.fromJson(json.decode(str));

List<ChildModel> childModelListFromJson(String str) => List<ChildModel>.from(
    json.decode(str)["data"].map((x) => ChildModel.fromJson(x)));

String childModelToJson(ChildModel data) => json.encode(data.toJson());
于 2021-01-19T09:13:56.363 回答