Flutter 向列表中添加新项目替换列表中的旧项目
当我向列表中添加新项时,它将覆盖先前的值。我想附加列表,但它没有发生Flutter 向列表中添加新项目替换列表中的旧项目,flutter,dart,Flutter,Dart,当我向列表中添加新项时,它将覆盖先前的值。我想附加列表,但它没有发生 Container( // color: Colors.red, child: Row( mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[ Container( width: 50, height: 50, // color: Colors.blue,
Container(
// color: Colors.red,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
width: 50,
height: 50,
// color: Colors.blue,
child: FlatButton(onPressed: (){
if(_itemCount > 0){
setState(() {
_itemCount--;
});
}
}, child: Image(image: AssetImage("images/minus.png"),width: 20,height: 20,),),
),
Container(
child: Text("$_itemCount"),
),
Container(
width: 50,
height: 50,
// color: Colors.green,
child: FlatButton(onPressed: () async{
setState(() {
_itemCount++;
});
cartItemList = [{"index":widget.intex,"itemObj":widget.items,"quantity":_itemCount}];
print(addedItems(cartItemList));
final prefs = await SharedPreferences.getInstance();
await prefs.setStringList('itemList', addedItems(cartItemList));
}, child: Image(image: AssetImage("images/plus.png"),width: 20,height: 20,),),
),
],
),
),
输出为
[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552,"category_id":12},"quantity":1}]
[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552,"category_id":12},"quantity":2}]
(数量增加)
[{"itemObj":{"item_price":20.0,"item_name":"English Breakfast","item_img":" ","item_code":"002","item_discount":0.0,"item_id":71,"category_id":12},"quantity":1}]
(餐厅的特色菜在哪里?)
但我想要像这样的输出
[{"itemObj":{"item_price":22.0,"item_name":"DINE SPECIAL BREAKFAST","item_img":" ","item_code":"001","item_discount":0.0,"item_id":552,"category_id":12},"quantity":2},{"item_price":20.0,"item_name":"English Breakfast","item_img":" ","item_code":"002","item_discount":0.0,"item_id":71,"category_id":12},"quantity":1}]
您每次单击按钮时都会设置列表,如下所示
cartItemList = [{"index":widget.intex,"itemObj":widget.items,"quantity":_itemCount}];
也许你需要做的是
cartItemList.add({"index":widget.intex,"itemObj":widget.items,"quantity":_itemCount});
您每次单击按钮时都会设置列表,如下所示
cartItemList = [{"index":widget.intex,"itemObj":widget.items,"quantity":_itemCount}];
也许你需要做的是
cartItemList.add({"index":widget.intex,"itemObj":widget.items,"quantity":_itemCount});