gpt4 book ai didi

flutter - Dart:使用 map 过滤列表

转载 作者:行者123 更新时间:2023-12-01 15:01:08 32 4
gpt4 key购买 nike

列表 1:我有一个包含用户 ID 的列表:(选定的用户)

['1234', '3455', '2330', '1111']

列表 2:我有第二个列表,其中包含用户的完整个人资料( map ):(所有用户)
[{"id": '1234', "name": "username"}, {"id": '3455' .... ]

列表 3:我想要包含所有选定用户的第三个列表:返回列表 2 中 ID 为列表 1 的所有用户。

我试过这个...
 list1.forEach((element) =>
list3.add(list2.where((data) => data['id'] == element)));

但这不起作用...
error: The argument type 'Iterable<Map<String, dynamic>>' can't be assigned to the parameter type 'Map<String, dynamic>'. 

我究竟做错了什么?

提前致谢!

最佳答案

您可以使用 list3 = list2.where((map)=>list1.contains(map["id"])).toList()用于创建列表 3

问题的原因是你的 list3 需要一张 map ,但你的 list2.where(..)代码是返回 List[map, map, map] .

关于flutter - Dart:使用 map 过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58671104/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com