在Flutter中,我得到了以下List<Map>
:
List<Map<String, dynamic>> recipeList = [
{
'name': 'rec1',
'id': 1,
'img': 'images/recipe.jpg',
'ingredients': [{
'name': 'salt',
'amount': '1',
'unit': '1',
},
{
'name': 'flour',
'amount': '100',
'unit': 'g',
},
{
'name': 'water',
'amount': '100',
'unit': 'g',
},
{
'name': 'milk',
'amount': '100',
'unit': 'g',
},],
},]
我将其向下传递给几个Widgets
,并希望在某个时刻将键值对{'didBuy':false}
添加到配料表中的每个Map
(基本上是{{1} }。
因此,我打电话给
recipeList['ingredients']
不幸的是,出现以下错误消息:List<Map<String, dynamic>> resultList = recipeList['ingredients'].map((elem) {
elem.addAll({'didBuy': false});
print(elem);
}).toList();
。
有人知道在没有得到此错误消息的情况下向地图添加内容的正确方法是什么吗?
更精确地编辑了问题。
EDIT2:
按照Hadrien的建议,在Dart Error: Unhandled exception:type '_InternalLinkedHashMap<String, bool>' is not a subtype of type 'Map<String, String>' of 'other'
内部显式调用List
的类型后,可以将键值对与布尔值相加。长期而言,我想从Internet上获取数据,所以我定义了RecipeObj:
Map
在这里,我明确声明了配料属性的类型,所以我认为我可以在(main)recipeList内部进行显式调用。但是,在通过一些小部件向下传递了成分属性后,flutter将其识别为class RecipeObj{
String name;
int id;
String img;
List<Map<String, dynamic>> ingredients;
RecipeObj(this.name, this.id, this.img, this.ingredients);
}
,尽管我到处都将其定义为List<Map<String, String>>
,为什么?
答案 0 :(得分:2)
dart用Map<String, String>
推断您的配料表的类型
您可以在列表中自行指定类型
'ingredients': <Map<String, dynamic>>[ {
'name': 'salt',
'amount': '1',
'unit': '1',
},
或在Map<String, dynamic>
函数中构建新的map
List<Map<String, dynamic>> resultList = recipeList['ingredients'].map((elem) {
final map = Map<String, dynamic>.from(elem);
map.addAll({'didBuy': false});
return map;
}).toList();
答案 1 :(得分:1)
这应该做
List<Map<String,dynamic>> recipeList = [
至少recipeList
和ingredients
指向同一集合实例。
var ingredients = recipeList;
答案 2 :(得分:0)
这是您需要的吗?
void main() {
List<Map> recipeList = [
{
'name': 'rec1',
'id': 1,
'img': 'images/recipe.jpg',
'ingredients': [{
'name': 'salt',
'amount': '1',
'unit': '1',
},
{
'name': 'flour',
'amount': '100',
'unit': 'g',
},
{
'name': 'water',
'amount': '100',
'unit': 'g',
},
{
'name': 'milk',
'amount': '100',
'unit': 'g',
},]
},];
print("[DATA BEFORE ANY CHANGE]");
print("recipeList.length=${recipeList.length}");
print("recipeList[0][\"ingredients\"]=${recipeList[0]["ingredients"]}");
print("recipeList[0][\"ingredients\"].last=${recipeList[0]["ingredients"].last}");
print("recipeList[0][\"ingredients\"].length=${recipeList[0]["ingredients"].length}");
// no recipe is worth if it doesn't contain chocolate
recipeList[0]["ingredients"].add({
'name': 'cocoa powder',
'amount': '200',
'unit': 'g',
});
print("\n\n[DATA AFTER ADD]");
print("recipeList[0][\"ingredients\"].last=${recipeList[0]["ingredients"].last}");
print("recipeList[0][\"ingredients\"].length=${recipeList[0]["ingredients"].length}");
}
输出
[DATA BEFORE ANY CHANGE]
recipeList.length=1
recipeList[0]["ingredients"]=[{name: salt, amount: 1, unit: 1}, {name: flour, amount: 100, unit: g}, {name: water, amount: 100, unit: g}, {name: milk, amount: 100, unit: g}]
recipeList[0]["ingredients"].last={name: milk, amount: 100, unit: g}
recipeList[0]["ingredients"].length=4
[DATA AFTER ADD]
recipeList[0]["ingredients"].last={name: cocoa powder, amount: 200, unit: g}
recipeList[0]["ingredients"].length=5