我建立的每个人都建立了一个休息 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 的所有孩子。我不知道如何快速实现这一点。任何帮助将不胜感激。