【问题标题】:Editing an existing List<String> item编辑现有 List<String> 项
【发布时间】:2020-02-15 09:04:00
【问题描述】:

我想编辑列表,但我不知道如何实现。

我在下面粘贴了我的代码

Container(
              child: ListView.builder(

                  itemCount: filterList.length,
                  shrinkWrap: true,
                  itemBuilder: (BuildContext context, int intex) {
                  //  return MenuList(filterList[intex]);

                    return Container
                      (
                      height:75,
                      //color: Colors.blue,
                      child: Card(
                        elevation: 1.0,
                        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(3.0)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Container(
                              child: Row(
                                children: <Widget>[
                                  Padding(
                                    padding: const EdgeInsets.only(left:8.0),
                                    child: Container(
                                      child: Image(image:AssetImage("images/beef_burger.jpg"),
                                        width: 70,height: 50,),
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.all(8.0),
                                    child: Container(
                                      child: Column(

                                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: <Widget>[

                                          Container(
                                            width: MediaQuery.of(context).size.width/2.2,
                                            //  color: Colors.blue,
                                            child: Text(filterList[intex].itemName,maxLines: 1,),
                                          ),
                                          Container(
                                            child: Text("AED ${filterList[intex].itemPrice}",style: TextStyle(color: Colors.grey),),
                                          ),
                                        ],
                                      ),
                                    ),
                                  )
                                ],
                              ),
                            ),

                            Container(
                              // color: Colors.red,
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.end,

                                children: <Widget>[
                                  Container(
                                    width: 50,
                                    height: 50,
                                    // color: Colors.blue,
                                    child: FlatButton(onPressed: (){

                                      if(filterList[intex].counter > 0){
                                        setState(() {
                                          filterList[intex].counter--;
                                        });

                                      }

                                    }, child: Image(image: AssetImage("images/minus.png"),width: 20,height: 20,),),
                                  ),
                                  Container(
                                    child: Text("${filterList[intex].counter}"),
                                  ),
                                  Container(
                                    width: 50,
                                    height: 50,
                                    //  color: Colors.green,
                                    child: FlatButton(onPressed: () async{

                                      setState(() {
                                        filterList[intex].counter++;
                                      });



                                      cartItemList.add({"itemObj":filterList[intex],"quantity":filterList[intex].counter});



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

                    );

                  }),
            ),

我已将列表转换为列表以存储在共享首选项中

List<String> addedItems(List<dynamic> cartList){


 try {
  var res = cartList.map((v) => 
  json.encode(v)).toList();
  listNew = res;
   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},"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}]


  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":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":"英式早餐" ,"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},"数量":2}]


【问题讨论】:

  • 您应该首先检查该项目是否存在于列表中,而不是一次又一次地添加。如果存在,则仅更新数量,否则添加项目。

标签: list flutter dart


【解决方案1】:

List 没有内置功能,只能添加独特的项目。 您可以使用地图或手动检查该项目是否已存在。

要检查你可以使用 indexWhere 方法: https://api.flutter.dev/flutter/dart-core/List/indexWhere.html

【讨论】:

    猜你喜欢
    • 2011-07-15
    • 2014-11-29
    • 2016-06-11
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    相关资源
    最近更新 更多