3
votes

I sit 6 Hours trying to add Map<dynamic, dynamic> Contains Map<dynamic, dynamic> inside Another Map<dynamic, dynamic>

How to add Map like this:

{
    map1:{
          map1_key1: map1_val1
          map1_key2: map1_val2
          map1_2:{
                  map1_3:{
                           map3_key1: map3_val1
                           map3_key1: map3_val2
                         }
                  map2_key1: map2_val1
                  map2_key1: map2_val2
                 }

          map1_key3: map1_val3
         }
}

repeatedly inside map called masterMap , and access every key/value inside it like any other map ?

  • i tried this
  • i tried this
  • i tried this
  • i tried this

    • tried addAll() function gives me error called on null..
    • tried addEntries() function gives me

      The argument type 'Map<dynamic, dynamic>' can't be assigned to the parameter type 'Iterable<MapEntry<dynamic, dynamic>>'
      

nothing work , i need help this is very bad and stupid thing , why there is no simple function to merge all map key/value with another one , that simple ?!

Update: my code:

Map<dynamic, dynamic> theSnapShot;
theSnapShot = {
  'map1': {
          '-LyUAD8B0LpKZ-5-cRn-': {
                   'CoordsMap': [null, {'FirstCoords': '30.088,31.279',}]
                                  }
          }
             };
Map<dynamic, dynamic> masterMap;
masterMap.addAll(Map.from(theSnapShot['map1']));

output:

Unhandled Exception: NoSuchMethodError: The method 'addAll()' was called on null.
3
can you please show your current code? - Sami Kanafani
i provided sample code .. i need to add this map under some key to list of maps - Almodoen
you mean that you have map like first code snippet and you need to add theSnapShot to that map right? - Blasanka
@Blasanka True ... - Almodoen

3 Answers

1
votes

it's simple
you have two choices right here, 1st : to define key then assign value to it

Map someThing = {
    "someThing2":{
      'key1': 'val1',
      'key2': 'val2',
      'key3': {},
    }
  };
// {someThing2: {key1: val1, key2: val2, key3: {}}}
someThing['someThing2']['key3'] = {
    'someThing3': {
          'key4': 'val4',
        },
  };
// {someThing2: {key1: val1, key2: val2, key3: {someThing3: {key4: val4}}}}

2nd option is to add the key and the value, just like the following

Map someThing = {
    "someThing2":{
      'key1': 'val1',
      'key2': 'val2',
    }
  };

someThing['someThing2'].putIfAbsent('key4', () => {
    'someThing3': {
          'key4': 'val4',
        },
  });
// {someThing2: {key1: val1, key2: val2, key4: {someThing3: {key4: val4}}}}
1
votes

Your out put Unhandled Exception: NoSuchMethodError: The method 'addAll()' was called on null. because of you havent initalized masterMap, just do this:

Map<dynamic, dynamic> masterMap = {};

I dont know it is solved your problem or not, Since, you said you dont know the key, I think below is what you are looking for:

Map<dynamic, dynamic> theSnapShot;
theSnapShot = {
  'map1': {
      '-LyUAD8B0LpKZ-5-cRn-': {
         'CoordsMap': [null, {'FirstCoords': '30.088,31.279',}]
      }
   }
};
Map<dynamic, dynamic> masterMap = Map.from(theSnapShot);
theSnapShot.forEach((key, value) {
    masterMap.addAll(theSnapShot[key]);
});

print(masterMap);

//output
//{map1: {-LyUAD8B0LpKZ-5-cRn-: {CoordsMap: [null, {FirstCoords: 30.088,31.279}]}}, -LyUAD8B0LpKZ-5-cRn-: {CoordsMap: [null, {FirstCoords: 30.088,31.279}]}}

Here is a simple example that I have created in dartpad.

0
votes

This a working version of your code

Map<dynamic, dynamic> theSnapShot;
theSnapShot = {
  'map1': {
          '-LyUAD8B0LpKZ-5-cRn-': {
                   'CoordsMap': [null, {'FirstCoords': '30.088,31.279',}]
                                  }
          }
             };
  Map<dynamic, dynamic> masterMap = theSnapShot;
  masterMap.addAll(theSnapShot["map1"]);
  print(masterMap);

basically it initializes the masterMap with the theSnapShot map and it appends the map1 entry to the masterMap. So the masterMap will look like at the end

{
    "map1": {
      "-LyUAD8B0LpKZ-5-cRn-": {"CoordsMap": [null, {"FirstCoords": "30.088,31.279"}]}},

    "-LyUAD8B0LpKZ-5-cRn-": {"CoordsMap": [null, {"FirstCoords": "30.088,31.279"}]}}

is this your desired output?