List 颤振/飞镖:如何在键等于的位置获取列表值

List 颤振/飞镖:如何在键等于的位置获取列表值,list,flutter,dart,key-value,List,Flutter,Dart,Key Value,我不知道为什么我很难找到答案,但我有一个列表,我需要从中获取键与特定条件匹配的值。钥匙都是独一无二的。在下面的例子中,我想得到颜色,其中名称等于“头痛”。结果应为“4294930176” 获取错误:类“\u InternalLinkedHashMap”没有实例获取程序“name”。有什么建议吗 编辑: 下面是我如何将数据添加到列表中,其中userDocuments取自Firestore集合: for (int i = 0; i < userDocument.length; i++) {

我不知道为什么我很难找到答案,但我有一个列表,我需要从中获取键与特定条件匹配的值。钥匙都是独一无二的。在下面的例子中,我想得到
颜色
,其中
名称
等于“头痛”。结果应为“4294930176”

获取错误:类“\u InternalLinkedHashMap”没有实例获取程序“name”。有什么建议吗

编辑: 下面是我如何将数据添加到列表中,其中userDocuments取自Firestore集合:

for (int i = 0; i < userDocument.length; i++) {
  var trendColorMap = {
     'name': userDocument[i]['name'],
     'color': userDocument[i]['color'].toString(),
  };
  trendsList.add(trendColorMap);
}
for(int i=0;i
我想,我知道问题出在哪里了。您犯了一个小错误,那就是,您试图将
Map
元素作为
对象
值调用

HashMap元素不能被称为
f.name
,它必须被称为
f['name']
。因此,以您的代码作为参考,这样做,您就可以开始了

String trendName = 'headache';
List trendsList = [{'name': 'fatigue', 'color': 4284513675}, {'name': headache, 'color': 4294930176}];

//What I'm trying
// You call the name as f['name']
int trendIndex = trendsList.indexWhere((f) => f['name'] == trendName);
print(trendIndex) // Output you will get is 1
Color trendColor = Color(int.parse(trendsList[trendIndex]['color'])); //same with this ['color'] not x.color
print(trendColor);

检查一下,让我知道这是否对你有帮助,我相信它会:)

只是为了检查一下,如果你没有这样做,你能让你的字典看起来像这样吗
[{'name':'failure','color':4284513675},{'name':'header','color':4294930176}
。然后再试一次,让我知道吗?我刚刚添加了代码,说明了如何将数据添加到列表中。也许你能告诉我如何正确地转换它@AlokHey,我已经回答了你的问题,检查一下,让我知道这可能是一个打字错误,但你必须将
s和
s写入'or'“。正如@Alok所写的,你使用过他的列表吗?嘿@SeriForte,他又犯了一个错误,用错误的方式调用
键来获取值,我已经演示了正确的方法。”。无论如何,感谢您的支持:)
String trendName = 'headache';
List trendsList = [{'name': 'fatigue', 'color': 4284513675}, {'name': headache, 'color': 4294930176}];

//What I'm trying
// You call the name as f['name']
int trendIndex = trendsList.indexWhere((f) => f['name'] == trendName);
print(trendIndex) // Output you will get is 1
Color trendColor = Color(int.parse(trendsList[trendIndex]['color'])); //same with this ['color'] not x.color
print(trendColor);