我有来自 JSON 的名为剧集的嵌套列表对象,那么如何根据 id 父对象而不是按位置对象获取剧集列表?



这是API JSON,所以我如何获取基于id项目对象的剧集列表,而不是按对象列表的位置

"results": [
{
"id": 5,
"name": "Wannous",
"description": "After yaqout the father left his wife and kids and disappeared for 20 years, the Devil (Wannous) came to his family claiming that he's their father's friend telling them that their father and (Wannous) own millions",
"portalId": "primax",
"type": "series",
"age": "70",
"releaseDate": "2020-05-18 00:00:00",
"viewsNum": 0,
"cCover1": "images/series/20200518140210_HgQ2G.jpg",
"cCover2": null,
"cPoster1": "images/series/20200518140210_CmkOf.jpg",
"cPoster2": null,
"mCover1": "images/series/20200518140210_erBqc.jpg",
"mCover2": null,
"mPoster1": "images/series/20200518140210_tkNGp.jpg",
"mPoster2": null,
"createdAt": "2020-05-18 14:02:10",
"updatedAt": null,
"movie": null,
"episodes": [
{
"id": 219,
"title": "Wannous",
"description": "After (Yaqout) the father left his wife and kids and disappeared for 20 years, the Devil (Wannous) came to his family claiming that he's their father's friend telling them that their father and (Wannous) own millions.",
"image": null,
"cover": "videos/2020_05_21/images/20200521193406_QkHD8.jpg",
"video": "videos/2020_05_21/20200521193406_wGIDS.mp4",
"promo": "videos/2020_05_21/20200521193406_vjQkW.jpg",
"displayAt": "2020-05-20 21:00:00",
"duration": "35:25",
"viewsNum": 0,
"position": 1,
"avgWatchTime": 0.0,
"likesNum": 0,
"portalId": "primax",
"createdAt": "2020-05-21 19:34:06",
"updatedAt": "2020-05-23 12:21:19",
"tags": []
}
],
"trailers": [],
"portalReleased": true
} ]

假设json是你从API得到的结果。 下面的代码应根据您传递给它ID从列表中返回一个map

Map getItemById(){
List items = json['results'];
Map itemById = items.where((item)=>item['id'] == 1).toList()[0];
return itemById;
}

使用 Quicktype.io 在 dart 中生成类转换器,以将 json 转换为对象/映射。将此类传递给用于从 API 检索数据的未来。现在,此数据作为结果的索引快照列表接收。 使用此索引,可以创建列表视图或从任何索引中检索项。

最新更新