Skip to content

Instantly share code, notes, and snippets.

@GIfatahTH
Created May 28, 2020 09:37
Show Gist options
  • Save GIfatahTH/b19a3ae95b9c0c806fc62649762cef71 to your computer and use it in GitHub Desktop.
Save GIfatahTH/b19a3ae95b9c0c806fc62649762cef71 to your computer and use it in GitHub Desktop.
@immutable
class CartState {
CartState({this.items});
// List of items in the cart.
final List<Item> items;
/// The current total price of all items.
int get totalPrice =>
items.fold(0, (total, current) => total + current.price);
/// Adds [item] to cart.
/// methods can be static
static CartState add(CartState currentState, Item item) {
return CartState(items: [...currentState.items, item]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment