addToCart method

Future<void> addToCart(
  1. CartItem cartItem
)

Adds an item to the user's cart in Firestore.

Parameters:

  • cartItem: The CartItem to be added to the cart.

Returns: A Future<void> that completes when the item is added.

Implementation

Future<void> addToCart(CartItem cartItem) async {
  try {
    await _firestore
        .collection('carts')
        .doc(userId)
        .collection('items')
        .doc(cartItem.productId)
        .set(cartItem.toMap());
  } catch (e) {
    print('Error adding to cart: $e');
  }
}