How to add a new pair to Map in Dart?
Asked Answered
J

6

132

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.

Jecho answered 24/12, 2018 at 1:28 Comment(3)
Is this code in a method? You can't have assignments or other code execute in the top level scopeRembrandt
Thank you for your comment. I didn't know that. It works.Jecho
dart is so weak when type conversion comes.Derek
A
205

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}
Autoradiograph answered 24/12, 2018 at 1:55 Comment(3)
Thank you for your reply, but It doesn't work and the same errors occur.Jecho
I tested the exact same code in the build function of my Widget, you're gonna have to add some more information to your question. Read more here: stackoverflow.com/help/how-to-askAutoradiograph
Thank you. I solved this issue. The cause of the error was that I executed the code in the top level scope, as @jonah-williams said at the comments of my question.Jecho
E
59

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.

Eavesdrop answered 20/4, 2019 at 2:25 Comment(0)
P
16
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.

Papery answered 3/4, 2021 at 8:53 Comment(0)
V
9

This way is also applicable:

Map<String, int> someMap = {
  "a": 1,
  "b": 2,
};
someMap.addEntries({"c":3}.entries);
Vaclava answered 18/9, 2022 at 9:24 Comment(0)
R
5

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;

Rowe answered 17/10, 2020 at 12:56 Comment(0)
S
1

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',}
Sixfold answered 9/5, 2021 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.