import 'package:flutter_one/models/catalog.dart';
import 'package:flutter/material.dart';
class CartModel {
// singleton class
static final cartModel = CartModel._internal();
catalog._internal();
factory CartModel() => cartModel;
// catalog field
CatalogModel _catalog;
// Collection of IDs - store Ids of each item
final List<int> _itemIds = [];
// Get Catalog
CatalogModel get catalog => _catalog;
set catalog(CatalogModel newCatalog) {
assert(newCatalog != null);
_catalog = newCatalog;
}
// get items in the cart
List<Item> get items => _itemIds.map((id) => _catalog.getById(id)).toList();
// get total price
num get totalPrice =>
items.fold(0, (total, current) => total + current.price);
// add item
void add(Item item) {
_itemIds.add(item.id);
}
// remove item
void remove(Item item) {
_itemIds.remove(item.id);
}
}
Non-nullable instance field '_catalog' must be initialized. (Documentation) Try adding an initializer expression, or add a field initializer in this constructor, or mark it 'late'. The name of a constructor must match the name of the enclosing class.