flutter
flutter

Reputation: 6786

convert list of maps to one map in dart?

If I have a list of key/value pairs, how would I convert them to a single map?

eg [{'ore': 'value'}, {'pure':'value'}, {'steel':'value}]. =>

{'ore': 'value',
'pure':'value'
'steel':'value'}

Upvotes: 0

Views: 1022

Answers (2)

lrn
lrn

Reputation: 71853

Just build a new map containing all the individual map's entries:

List<Map<Something, Other>> listOfMaps = ...;

var combinedMap = {for (var map in listOfMaps) ...map};

Upvotes: 5

Stephen
Stephen

Reputation: 4249

Use the reduce function

var v =[{'ore': 'value'}, {'pure':'value'}, {'steel':'value'}];

var w = v.reduce((a,b){
  a.addAll(b);
  return a;
});
  

Upvotes: 3

Related Questions