Created
May 28, 2020 09:37
-
-
Save GIfatahTH/b19a3ae95b9c0c806fc62649762cef71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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