Home > Net >  Add value exists in the key - Flutter
Add value exists in the key - Flutter

Time:06-17

I have the following code and I would like to add to the value already exists in the key:

 final dataMap = <String, double>{};
 json.forEach((myKey) => {
    dataMap.putIfAbsent(myKey["sigla"], () => 1),
 });

In the example above, I am assigning 1, but if the key already exists and its value was 2, I would like to add 1 more, that is, the value would be 3.

How to do this?

Thanks.

CodePudding user response:

You can update itand if absent set it to 0

json.forEach((myKey){
    dataMap.update(myKey['sigla'], (myKey['keyofDouble']) =>   myKey['keyofDouble'], ifAbsent: () => 1);
  });
  • Related