Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter – Add maps to a map

Suppose there are two maps.

Map map01 = {
  'name': 'Peter',
  'age': 30,
}

Map map02 = {
  'name': 'Mark',
  'age': 25,
}

And there’s another map called Map map1.

I need to add these map01 and map02 to map1.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i. e.

Map map1 = {
  map01: {
    'name': 'Peter',
    'age': 50,
  },
  map02: {
    'name': 'Mark',
    'age': 25,
  },
}

How can I do this?

>Solution :

The key isn’t getting from variable name. It is generating from map${(i + 1).toString().padLeft(2, "0")}.

You can do something like this

    Map map01 = {
      'name': 'Peter',
      'age': 30,
    };

    Map map02 = {
      'name': 'Mark',
      'age': 25,
    };

    final myMaps = [map01, map02];
    final result = {};

    for (int i = 0; i < myMaps.length; i++) {
      final k = "map${(i + 1).toString().padLeft(2, "0")}";
      result.addAll({k: myMaps[i]});
    }
    print(result);
    //{map01: {name: Peter, age: 30}, map02: {name: Mark, age: 25}}
  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading