我正在尝试将地图转换为列表。我正在尝试使用forEach()方法。但是我不知道我要去哪里错了,有人可以帮忙吗?同样在将它们转换为列表之后,我想访问它们的索引。怎么办?
例如:我有2张地图
Map foodMap = {
value:50
title:'food',
}
Map travelMap={
value:30,
title:'travel'
}
List<String> sampleList = [];
我想将我的两个地图转换为list并将其添加到sampleList。
答案 0 :(得分:0)
Map foodMap = {
"value": 50,
"title": 'food',
};
Map travelMap = {
"value": 30,
"title": 'travel',
};
Map<String, double> sampleList = {};
我认为,这就是您要添加两个地图的方式:
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);
要将地图列表合并为单个
List<Map> mapList = [/*Contains list of maps*/];
....
mapList.forEach((mapItem) => _addToMappedList(sampleList, travelMap))
访问他们的索引
print(sampleList.entries.elementAt(1));