我想知道如何从 map 内的值获取列表中的特定 map 。例如,如果我有:
List<Map> aList = [{'id':'1'},{'id':'8'},{'id':'31'}];
如何获取 id == 8 的 map ?
我正在 DartPad 上尝试,但在将结果分配给列表时总是遇到错误,并且没有找到有关如何正确将WhereIterable分配给列表的答案。
void main() {
List map = [
{'id':'pez', 'num': 25},
{'id':'lava', 'num': 3},
{'id':'mar', 'num': 81},
{'id':'pan', 'num': 33},
{'id':'lava', 'num': 73},
];
List otherMap = map.where((item) => item['id'] == 'mar');
print(otherMap);
}
//ERROR:
Uncaught Error: TypeError: Instance of 'WhereIterable<dynamic>': type 'WhereIterable<dynamic>' is not a subtype of type 'List<dynamic>'
最佳答案
必须在 where()
之后使用 .toList()
方法
List otherMap = map.where((item) => item['id'] == 'mar').toList();
关于flutter - 如何从Map中的值中选择List中的特定Map?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59166616/