I have a bug and I don't see where is the problem.
Context -> I have created a method so the user can increase and decrease the items in the card by pressing a button or by adding the number manually.
The problem comes when the user modifies the number of items manually.
This is the method:
Future<void> setProductQuantity({
@required Product product,
@required String cartId,
@required int quantity,
}) async {
Cart cart = getCart(cartId) ?? _createCart(cartId: cartId, products: [product]);
Product productInCart = cart.getProductForId(product.id);
if (productInCart != null) {
if (quantity == 0) {
cart.products.remove(productInCart);
} else {
productInCart.quantity = quantity;
}
} else {
product.quantity = quantity;
cart.products.add(product);
}
await persistCart(cart);
}
The problem is if one of the items is more than 0 and the rest are 0, the items with 0 quantity gets added as well to the card. I would like to avoid that to happen.
Can anyone see where the problem is?
CodePudding user response:
Inside the else condition check for the quantity and add it to the cart only if it's higher than 0. That way you any item with quality 0 will not be added to the cart.