无法在Flutter中修改列表项/元素 [英] Cannot modify a List Item/Element in Flutter
问题描述
我有一个初始化如下的列表: 这在main之外(因此它是全局的,因为许多其他类都需要访问它).
I have a list that is initialised as follows: This is outside of main (so it is global as many other classes need to access it).
List mainMenuList = [];
然后在其中填充购物物品.
It is then populated with shopping items.
如此
mainMenuList.elementAt[counter].shoppingPrice;
或
print(mainMenuList.elementAt[counter].shoppingPrice);
给出"4.00";
但是如果我想增加或更改该元素的值,则会收到错误消息:
but if I wanted to increment or change that element's value, I get an error:
flutter: Unsupported operation: read-only
因此,例如,我正在尝试为其添加补充:
So, for example, I'm trying to add supplements to it:
mainMenuList[listItemIndicator]['ItemPrice'] += supplementsList[supplementCounter]['SupplementPrice']);
我收到以下错误:
flutter:不支持的操作:只读
flutter: Unsupported operation: read-only
所以即使我忘记了补品..如果我做了类似的事情:
So even if I forget about supplements.. if I did something like:
mainMenuList[listItemIndicator]['ItemPrice'] = 1.00;
我遇到同样的错误
非常感谢!
推荐答案
您尝试在此处将List
与键值泛型配合使用,以便上面的代码显示
You are trying to use key-value generic with List
here or so the above code shows
mainMenuList[listItemIndicator]['ItemPrice'] = 1.00; //incorrect way of using List
您需要为此使用HashMap
或LinkedMap
.
这篇关于无法在Flutter中修改列表项/元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!