Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 向列表中添加新项目替换列表中的旧项目_Flutter_Dart - Fatal编程技术网

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});