首页 > 解决方案 > 通过在颤振中删除提供程序类中的重复条目来更新购物车

问题描述

我正在使用购物车应用程序并使用 flutter-Provider 更新我的购物车。一切正常,但我的购物车列表中出现重复项目,如何删除这些重复条目并添加数量。我想获取包含项目和数量的列表。请帮我处理这段代码,我可以在哪里添加逻辑并过滤列表中删除的重复项。

这是我的代码:

购物车列表.dart:

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:

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

}

这里我使用了包含:

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

  }

标签: flutterdartflutter-provider

解决方案


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

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

推荐阅读