将新项目添加到列表中,替换列表颤振中的旧项目

时间:2020-02-10 13:48:12

标签: list flutter dart

当我将新项目添加到列表中时,它将覆盖先前的值。我想追加列表,但没有发生

  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,),),
              ),
            ],
          ),
        ),

我有一个listview,listview项将从api中获取,当我单击一个项目时,我希望它添加到列表中,我可以将其保存在sharedpreference中。但每次我单击较早的按钮时,都会被新按钮覆盖。

下面是将数据转换为json格式的代码

  List<String> addedItems(List<dynamic> cartList){
try {
  var res = cartList.map((v) => json.encode(v)).toList();
  return res;
} catch (err) {
  // Just in case
  return [];
}
}

输出为 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},“数量”:1}]

  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},“数量”:2}]

  1. 当我添加新项目时

[{“ itemObj”:{“ item_price”:20.0,“ item_name”:“英式早餐”,“ item_img”:“”,“ item_code”:“ 002”,“ item_discount”:0.0,“ item_id”: 71,“ category_id”:12},“数量”: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”:“英式早餐”,“ item_img”:“”,“ item_code”:“ 002”,“ item_discount “:0.0,” item_id“:71,” category_id“:12},”数量“:1}]

1 个答案:

答案 0 :(得分:0)

您每次点击这样的按钮都会设置列表

cartItemList = [{"index":widget.intex,"itemObj":widget.items,"quantity":_itemCount}];

也许您需要做的是

cartItemList.add({"index":widget.intex,"itemObj":widget.items,"quantity":_itemCount});