簡體   English   中英

如何從 json 列表中過濾 id 並將其存儲在 flutter 的另一個列表中?

[英]How to filter id from json list and store in another list in flutter?

我有 3 個資格的 json 列表。 從我必須過濾 id 並將其存儲到另一個列表中,作為 widget.eligibility 的 json 列表如下

[{id: 13, category_id: 9, title: Must be over the age of 18 to assist, details: Must be over the age of 18 to assist, status: 1, created_at: 2022-08-06T10:26:59.000000Z, updated_at: 2022-08-06T10:26:59.000000Z, pivot: {opportunity_id: 15, eligibility_id: 13}}, {id: 14, category_id: 9, title: Must be a female, details: Must be a female, status: 1, created_at: 2022-08-06T10:27:17.000000Z, updated_at: 2022-08-06T10:27:17.000000Z, pivot: {opportunity_id: 15, eligibility_id: 14}}, {id: 15, category_id: 9, title: Must be able to lift my wheelchair/20 kg, details: Must be able to lift my wheelchair/20 kg, status: 1, created_at: 2022-08-06T10:27:32.000000Z, updated_at: 2022-08-06T10:27:32.000000Z, pivot: {opportunity_id: 15, eligibility_id: 15}}]

我的代碼

 List<int> eligibility = [];
setState(() {
  eligibility = (widget.eligibility.where((element) => element.id)).toList();
});
print(eligibility);

.add() Function 用於在列表中插入值

你的代碼應該是這樣的

List<int> eligibility = [];

for(Map map in widget.eligibility){          // where the widget.eligibility is came from previous widget which seems a list of map in your code
  eligibility.add(map['id']);
}

setState((){});                              // set state after creating a list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM