Reputation: 181
I am wondering how one can get a specific map in a list from a value inside the map. For example, if I have:
List<Map> aList = [{'id':'1'},{'id':'8'},{'id':'31'}];
How can I get the Map whose id == 8?
I am trying it on DartPad, but I always get an error when assigning the result to a List and have found no answers on how to properly assign a WhereIterable to a List.
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>'
Upvotes: 2
Views: 1593
Reputation: 181
One must use the .toList()
method after where()
List otherMap = map.where((item) => item['id'] == 'mar').toList();
Upvotes: 2