Files
retail/lib/features/home/presentation/providers/cart_item_count_provider.dart
Phuoc Nguyen b94c158004 runable
2025-10-10 16:38:07 +07:00

28 lines
759 B
Dart

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'cart_provider.dart';
part 'cart_item_count_provider.g.dart';
/// Provider that calculates total number of items in cart
/// This is optimized to only rebuild when the count changes
@riverpod
int cartItemCount(Ref ref) {
final itemsAsync = ref.watch(cartProvider);
return itemsAsync.when(
data: (items) => items.fold<int>(0, (sum, item) => sum + item.quantity),
loading: () => 0,
error: (_, __) => 0,
);
}
/// Provider that calculates unique items count in cart
@riverpod
int cartUniqueItemCount(Ref ref) {
final itemsAsync = ref.watch(cartProvider);
return itemsAsync.when(
data: (items) => items.length,
loading: () => 0,
error: (_, __) => 0,
);
}