Cart Provider¶
Source: pharmanet/lib/core/providers/cart_provider.dart
Provider Reference¶
| Provider | Type | Description |
|---|---|---|
cartProvider |
NotifierProvider<CartNotifier, List<CartItem>> |
Cart items with actions |
Notifier Actions¶
| Method | Description |
|---|---|
addItem(product, quantity) |
Add or increment quantity |
removeItem(productId) |
Remove from cart |
updateQuantity(productId, quantity) |
Set quantity |
clearCart() |
Empty cart |
loadCart() |
Sync from Supabase on app start |
The notifier keeps the Supabase cart_items table in sync with every mutation.
CartItem Model¶
@freezed
class CartItem with _$CartItem {
const factory CartItem({
required Product product,
@Default(1) int quantity,
}) = _CartItem;
}
Usage¶
final cart = ref.watch(cartProvider);
final cartNotifier = ref.read(cartProvider.notifier);
// Add to cart
cartNotifier.addItem(product, 1);
// Get total items
final itemCount = cart.fold<int>(0, (sum, item) => sum + item.quantity);
Related¶
- Cart & Checkout Feature — UI
- Cart Model — Data model