Creating a shopping cart application with Riverpod
Create a new Flutter app named cart_riverpod
and copy item.dart
into your lib
folder, as we have been doing in the previous sections. Add dependencies for equatable
and flutter_riverpod
from pub.dev. Let's get started, as follows:
- Create a new file named
cart_model.dart
and add the following code to it:import 'item.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; final cartProvider = StateNotifierProvider((ref) => CartNotifier()); class CartNotifier extends StateNotifier<CartModel> { CartNotifier() : super(CartModel(cart: [])); void addToCart(Item item) { var updatedCart = state.cart; updatedCart.add(item); state = CartModel(cart: updatedCart); } void removeFromCart(Item item) { var updatedCart = state.cart; updatedCart...