Combine/merge multiple maps into 1 map
you can use addAll
method of Map
object
var firstMap = {"1":"2"};
var secondMap = {"2":"3"};
var thirdMap = {};
thirdMap.addAll(firstMap);
thirdMap.addAll(secondMap);
print(thirdMap);
Or
var thirdMap = {}..addAll(firstMap)..addAll(secondMap);
Update
Since dart sdk 2.3
You can use spread operator ...
final firstMap = {"1":"2"};
final secondMap = {"2":"3"};
final thirdMap = {
...firstMap,
...secondMap,
};
alternate syntax using Map.addAll
, Iterable.reduce
and cascading operator, for combining a lot of maps:
var combinedMap = mapList.reduce( (map1, map2) => map1..addAll(map2) );
live dartpad example https://dartpad.dartlang.org/9cd116d07d2f45a9b890b4b1186dcc5e