setCart method

Future<void> setCart(
  1. Cart cart
)

Set cart Automatically sends a push request with updated cart and wishlist state if cart was previously initialized

Implementation

Future<void> setCart(final Cart cart) async {
  final storage = HiveStorageService.instance;
  String currentCart = jsonEncode(cart.toMap());
  String? previousCart = await storage.getCartData();

  if (previousCart == null || previousCart != currentCart) {
    await storage.setCartData(currentCart);
    _cartChanged = true;

    // Only send automatic push if cart was already initialized (not first load)
    if (_cartInitialized) {
      // Send push with current state (cart + wishlist) without page context
      await trackScreenView();
      debugPrint('Automatic cart state push sent');
    } else {
      _cartInitialized = true;
      debugPrint('Cart initialized (no automatic push on first load)');
    }
  }

  // Mark as initialized if cart data didn't change but not yet initialized
  if (!_cartInitialized) {
    _cartInitialized = true;
    debugPrint('Cart initialized with existing data (no automatic push on first load)');
  }
}