Creating a shopping cart application with MobX
Create a new Flutter application named cart_mobx
and copy item.dart
into your lib
folder, as we have been doing in the previous sections. Add dependencies for equitable
, flutter_mobx
, and mobx
under dependencies
inside pubspec.yaml
. Also, add dependencies for build_runner
and mobx_codegen
under dev_dependencies
in pubspec.yaml
. Let's get started, as follows:
- Create a new file named
cart.dart
and add the following code to it:import 'package:mobx/mobx.dart'; import 'item.dart'; part 'cart.g.dart'; class Cart = _Cart with _$Cart; abstract class _Cart with Store { List<Item> items = populateItems(); @observable List<Item> cart = []; @action void addItemToCart(Item item) { var newCart = <Item>[]; newCart.addAll(cart); newCart.add(item); ...