gpt4 book ai didi

list - flutter/Dart : How to get list value where key equals

转载 作者:行者123 更新时间:2023-12-03 03:21:49 25 4
gpt4 key购买 nike

我不知道为什么我很难找到答案,但我有一个列表,我需要从键匹配特定条件的地方获取值。 key 都是独一无二的。在下面的例子中,我想得到 color哪里name相当于“头痛”。结果应该是“4294930176”。

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

//What I'm trying
int trendIndex = trendsList.indexWhere((f) => f.name == trendName);
Color trendColor = Color(int.parse(trendsList[trendIndex].color));
print(trendColor);

我得到的错误:“_InternalLinkedHashMap”类没有实例 getter “名称”。有什么建议?

编辑:
这是我将数据添加到列表的方式,其中 userDocuments 取自 Firestore 集合:
for (int i = 0; i < userDocument.length; i++) {
var trendColorMap = {
'name': userDocument[i]['name'],
'color': userDocument[i]['color'].toString(),
};
trendsList.add(trendColorMap);
}

最佳答案

我想,我明白了问题所在。你犯了一个小错误,那就是,你试图调用 Map元素作为 object值(value)。

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);

检查一下,如果这对您有帮助,请告诉我,我相信它会:)

关于list - flutter/Dart : How to get list value where key equals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62366007/

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