How to add a new pair to Map in Dart?

FlutterDartKey PairFluttermap

Flutter Problem Overview


I caught the following errors when adding a new pair to a Map.

  • Variables must be declared using the keywords const, final, var, or a type name
  • Expected to find;
  • the name someMap is already defined

I executed the following code.

Map<String, int> someMap = {
  "a": 1,
  "b": 2,
};

someMap["c"] = 3;

How should I add a new pair to the Map?

I'd also like to know how to use Map.update.

Flutter Solutions


Solution 1 - Flutter

To declare your map in Flutter you probably want final:

final Map<String, int> someMap = {
  "a": 1,
  "b": 2,
};

Then, your update should work:

someMap["c"] = 3;

Finally, the update function has two parameters you need to pass, the first is the key, and the second is a function that itself is given one parameter (the existing value). Example:

someMap.update("a", (value) => value + 100);

If you print the map after all of this you would get:

{a: 101, b: 2, c: 3}

Solution 2 - Flutter

You can add a new pair to a Map in Dart by specifying a new key like this:

Map<String, int> map = {
  'a': 1,
  'b': 2,
};

map['c'] = 3;  // {a: 1, b: 2, c: 3}

According to the comments, the reason it didn't work for the OP was that this needs to be done inside a method, not at the top level.

Solution 3 - Flutter

Map<String, dynamic> someMap = {
  'id' : 10,
  'name' : 'Test Name'
};
someMethod(){
  someMap.addAll({
    'email' : '[email protected]'
  });
}
printMap(){
  print(someMap);
}

make sure you can't add entries right below the declaration.

Solution 4 - Flutter

Another way to add new key/value as map to existing map is this,

oldMap.addEntries(myMap.entries);

This will update the oldMap with key/value of myMap;

Solution 5 - Flutter

I write this utility:

Map updateMap({
  /// Update a map with another map
  /// Example:
  ///   Map map1 = {'name': 'Omid', }
  ///   Map map2 = {'family': 'Raha', }
  ///   Map map = updateMap(data:map1, update:map2);
  /// Result:
  ///   map = {'name': 'Omid', 'family': 'Raha',}
  @required Map data,
  @required Map update,
}) {
  if (update == null) return data;
  update.forEach((key, value) {
    data[key] = value;
  });
  return data;
}

Example:

Map map1 = {'name': 'Omid', }
Map map2 = {'family': 'Raha', }

Map map = updateMap(data:map1, update:map2);

print(map);

{'name': 'Omid', 'family': 'Raha',}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhrsma2iView Question on Stackoverflow
Solution 1 - FlutterTylerView Answer on Stackoverflow
Solution 2 - FlutterSuragchView Answer on Stackoverflow
Solution 3 - FlutterAbdul QadirView Answer on Stackoverflow
Solution 4 - FlutterRopali MunshiView Answer on Stackoverflow
Solution 5 - FlutterOmid RahaView Answer on Stackoverflow