How to convert List of Objects to Map and use the object's property as key?

Viewed 1032

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;
}
1 Answers

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