runable
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
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,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user