首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过在flutter中删除provider类中的重复条目来更新购物车

通过在flutter中删除provider类中的重复条目来更新购物车
EN

Stack Overflow用户
提问于 2020-06-30 13:20:36
回答 1查看 474关注 0票数 0

我正在使用购物车应用程序,并使用flutter-Provider更新我的购物车。一切正常,但是我的购物车列表中有重复的项目,我如何删除这些重复的条目并添加数量。我想要一份清单,上面有项目和数量。请帮助我这段代码,我可以在哪里添加逻辑,并过滤删除重复项目的列表。

以下是我的代码:

CartList.dart:

代码语言:javascript
复制
class CartList extends ChangeNotifier{
  List<CategoryItems> cartList = [];
  double _totalPrice = 0.0;

  addCartItem(CategoryItems categoryItems){
    cartList.add(categoryItems);
    categoryItems.Counter++;
    _totalPrice+=double.parse(categoryItems.OurPrice);
    notifyListeners();

  }



  removeCartItem(CategoryItems categoryItems){
    _totalPrice-=double.parse(categoryItems.OurPrice);
    categoryItems.Counter--;
    if(categoryItems.Counter<1){
      categoryItems.ShouldVisible = false;
    }

    cartList.remove(categoryItems);
    notifyListeners();
  }

  int get count{
    return cartList.length;

  }

  double get totalPrice{
    return _totalPrice;
  }



  List<CategoryItems> get basketItem{
    return cartList;
  }

  incrementCounter(CategoryItems categoryItems){
    categoryItems.Counter++;
    notifyListeners();
  }
  decrementCounter(CategoryItems categoryItems)
  {
    categoryItems.Counter--;
    notifyListeners();
  }



}

CategoryItem.dart:

代码语言:javascript
复制
class CategoryItems{
  String CategoryName;
  int Counter;
  String MarketPrice;
  String Name;
  String OurPrice;
  bool ShouldVisible;
  String TotalDiscount;
  String Weight;
  int ID;

  String get getCategoryName => CategoryName;
  int get getCounter => Counter;
  String get getMarketPrice => MarketPrice;
  String get getName => Name;
  String get getOurPrice => OurPrice;
  bool get getShouldVisible => ShouldVisible;
  String get getTotalDiscount => TotalDiscount;
  String get getWeight => Weight;
  int get getID => ID;

  CategoryItems(
      this.CategoryName,
      this.Counter,
      this.MarketPrice,
      this.Name,
      this.OurPrice,
      this.ShouldVisible,
      this.TotalDiscount,
      this.Weight,
      this.ID
      );

}

这里我使用了Contain:

代码语言:javascript
复制
  addCartItem(CategoryItems categoryItems){
     if(cartList.contains(categoryItems.ID)){
       categoryItems.Counter++;
       _totalPrice+=double.parse(categoryItems.OurPrice);
     }else{
    cartList.add(categoryItems);
    categoryItems.Counter++;
    _totalPrice+=double.parse(categoryItems.OurPrice);
    notifyListeners();}

  }
EN

回答 1

Stack Overflow用户

发布于 2020-07-01 18:44:24

我以这种方式迭代购物车存储桶并检查ontap中的冗余值:

代码语言:javascript
复制
                                                                      bool itemFound = false;
                                                                      for(var i=0;i<CART.count;i++)
                                                                        {
                                                                         if(CART.count>=1 && categoryItemList[index].ID==CART.basketItem[i].ID){
                                                                           itemFound = true;
                                                                           break;
                                                                         }else{
                                                                           print('Item Not Exists');
                                                                         }
                                                                        }
                                                                      if(itemFound == true){
                                                                        print(itemFound);
                                                                        CART.addQuantityOnly(categoryItemList[index]);
                                                                      }else{
                                                                        print(itemFound);
                                                                        CART.addCartItem(categoryItemList[index]);
                                                                      }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62650756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档