addToCart method

void addToCart({
  1. required CartModel cartModel,
})

This method is called when we have to add an item to the cart Example: void addToCart(YourProductModel product) { flutterCart.addToCart( cartModel: CartModel( // ... other parameters if discount is applicable and in percentage so, you can calculate the discount like this var discount = (product.discountPercentage / 100) * product.price; discount: discount, productMeta takes Map<String, dynamic> so, you can store your complete product data in productMeta productMeta: product.toJson()), ); }

Implementation

void addToCart({required CartModel cartModel}) {
  var existingItemIndex =
      getProductIndex(cartModel.productId, cartModel.variants);

  if (existingItemIndex != -1) {
    var quantity =
        _cartItemsList[existingItemIndex].quantity + cartModel.quantity;
    _cartItemsList[existingItemIndex] =
        _cartItemsList[existingItemIndex].copyWith(quantity: quantity);
  } else {
    _cartItemsList.add(cartModel);
  }
  if (getPersistenceSupportStatus()) {
    _updatePersistenceCart(_cartItemsList);
  }
}