Home > Mobile >  I need to increase the quantity on cart screen and also decrease the quantity also with a and - ic
I need to increase the quantity on cart screen and also decrease the quantity also with a and - ic

Time:07-02

    void addToCart(ProductModel product) {
            if (cartItems.contains(product)) {
              int index = cartItems.indexOf(product);
              cartItems.remove(product);
              product.quantity  = 1;*/Error on this line/*
              cartItems.insert(index, product);
            } else {
              if (product.quantity == 0) {
                product.quantity  = 1;*/Error on this line/*
              }
              cartItems.add(product);
            }
          }
   void decreaseQuantity(ProductModel product) {
    if (cartItems.contains(product)) {
      int index = cartItems.indexOf(product);
      cartItems.remove(product);
      if (product.quantity > 1) {
        product.quantity -= 1*/Erroro on here/*;
        cartItems.insert(index, product);
      }
    }
  }

Error

The method ' ' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').

CodePudding user response:

use

product.quantity  ;

hope this helpts

CodePudding user response:

3 solutions are available to you:

  • Add !

    product.quantity! = 1;

  • Initialize the quantity by 0 in the ProductModel

  • Change the type of the quantity by dynamic

One of them can help

  • Related