Home > Back-end >  Cart item is not updated as per logic
Cart item is not updated as per logic

Time:09-24

I want to update the quantity of an added product. My logic follows as if an item is already added on the cart and the user adds it again, it just updates the quantity. But it doesn't.

As per @csisanyi I updated my code with onCart[itemIndex].quantity = 1; but it still shows quantity as 1

List<CartModel> onCart = <CartModel>[].obs;
  

  void addtoCart(CartModel item) {
    if (onCart.contains(item.productID)) {
      var itemIndex = onCart.indexOf(item);
      onCart.insert(
        itemIndex,
        CartModel(
          price: item.price,
          productID: item.productID,
          productName: item.productName,
          quantity: item.quantity   1,
        ),
      );
      print(item.productName   item.quantity.toString());
    } else {
      onCart.add(item);
    }

  }

GestureDetector(
 onTap: () {
                            CartController().addtoCart(CartModel(
                              price: productController.products[index].price,
                              productID: productController.products[index].id,
                              quantity: 1,
                              productName: productController.products[index].flavor,
                            ));
                          },
                          child: Icon(Icons.shopping_cart_outlined)),

CodePudding user response:

List .insert() adds a new element to the list at position index.

Instead of inserting, you should probably try

onCart[itemIndex].quantity  = 1;

CodePudding user response:

Please have a look at this sample code

import 'dart:core';

class Product {
  int? id;
  String? name;
  int? price;
  int? quantity;
  Product(this.id, this.name, this.price, this.quantity);
}

List<Product>? cart = [];

void addToCart(Product product) {
  if (cart!.any((element) => element.id == product.id)){
    product.quantity = product.quantity!   1;
    cart?[cart!.indexOf(product)] = product;
  }else{
    cart?.add(product);
  }
  notifyUpdate(product);
}

notifyUpdate(Product product) {
  print('Added ' product.name.toString() ' to the Cart');
}

void main() {
  Product apple = Product(1, "Apple", 100, 1);
  Product banana = Product(2, "Banana", 200, 1);
  Product orange = Product(3, "Orange", 300, 1);
  Product mango = Product(4, "Mango", 400, 1);
  Product pineapple = Product(5, "Pineapple", 500, 1);
  Product watermelon = Product(6, "Watermelon", 600, 1);
  Product strawberry = Product(7, "Strawberry", 700, 1);
  Product grapes = Product(8, "Grapes", 800, 1);

  addToCart(apple);
  addToCart(banana);
  addToCart(banana);
  addToCart(orange);
  addToCart(orange);
  addToCart(orange);
  addToCart(mango);
  addToCart(pineapple);
  addToCart(watermelon);
  addToCart(watermelon);
  addToCart(strawberry);
  addToCart(grapes);
  print('\n');
  cart?.forEach((element) {
    print(element.name.toString()   ' ('   element.quantity.toString()   ')');

  });
}

Code Output

usamasarwar@usama Temp % dart "/Users/usamasarwar/Temp/cart.dart"
Added Apple to the Cart
Added Banana to the Cart
Added Banana to the Cart
Added Orange to the Cart
Added Orange to the Cart
Added Orange to the Cart
Added Mango to the Cart
Added Pineapple to the Cart
Added Watermelon to the Cart
Added Watermelon to the Cart
Added Strawberry to the Cart
Added Grapes to the Cart


Apple (1)
Banana (2)
Orange (3)
Mango (1)
Pineapple (1)
Watermelon (2)
Strawberry (1)
Grapes (1)
  • Related