2

I am trying to convert a map into a list. I'm trying to use the forEach() method.But i don't know where i'm going wrong.Can someone help? Also after converting them to lists,i want to access their index.How to do?

For eg: I have 2 maps

Map foodMap = {
     value:50
     title:'food',
}

Map travelMap={
     value:30,
     title:'travel'
}

List<String> sampleList = [];

I want to convert my two maps into list and add them into sampleList.

cmd_prompter
  • 1,566
  • 10
  • 16
Aishwarya
  • 509
  • 1
  • 7
  • 14

1 Answers1

0
Map foodMap = {
  "value": 50,
  "title": 'food',
};

Map travelMap = {
  "value": 30,
  "title": 'travel',
};

Map<String, double> sampleList = {};

I think, this is how you want to add two maps:

void _addToMappedList(Map<String, double> sampleList, Map newData) {
  String title = newData["title"];
  double value = double.tryParse(newData["value"].toString());

  sampleList[title] = value;
}

....

_addToMappedList(sampleList, foodMap);
_addToMappedList(sampleList, travelMap);

To merge list of maps to single

List<Map> mapList = [/*Contains list of maps*/];
....
mapList.forEach((mapItem) => _addToMappedList(sampleList, travelMap))

To access their index

print(sampleList.entries.elementAt(1));
Crazy Lazy Cat
  • 13,595
  • 4
  • 30
  • 54