Reputation: 5385
I have list of coin objects, I want to cast the list to the map. How can I use id's as a Map key?
class Coin {
int id;
String groupID;
String symbol;
int coinOrder;
String fullName;
}
Upvotes: 2
Views: 1653
Reputation: 13652
For Flutter 3 >
final Map<int, Coin> coinMap = {
for (var coin in coins)
coin.id: coin
};
Upvotes: 0
Reputation: 7045
There is a fromIterable
constructor for Map class. You can use it to convert a list to a map.
Map<int, Coin> map = Map.fromIterable(list, key: (item) => item.id, value: (item) => item);
Upvotes: 4