Creating a cart app with GetIt
Let's create our cart application using GetIt. You will need to create a new Flutter app named cart_get_it
, add a dependency for get_it
and equatable
from pub.dev, and copy the item.dart
file from the previous section's code into your lib
folder. Let's get started:
- Create a new file named
cart_model.dart
and add the following code:import 'package:flutter/material.dart'; import 'item.dart'; import 'main.dart'; // One class CartModel extends ChangeNotifier { // Two List<Item> items = populateItems(); List<Item> cart = []; // Three void addItemToCart(Item item) { var newCart = <Item>[]; newCart.addAll(cart); newCart.add(item); cart = newCart; notifyListeners(); } removeItemFromCart(Item...