简体   繁体   中英

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

I have a json list of 3 eligibilities. From where i have to filter id and store it to another list, the json list as widget.eligibility is below

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

my code

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

.add() Function are use to insert value in a list

your code should be like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM