Now that we know what we're trying to build, let's figure out how to build it.
This diagram shows all the required components:
We need three repositories:
LocalCartRepository: so we can use local storage when we add items to the cart as a guestRemoteCartRepository: so we can use a remote database when we add items to the cart as a signed-in userAuthRepository: to figure out if we're signed-in or notWe have these two widgets called AddToCart and ShoppingCartItem that can be used to modify the shopping cart contents.
In addition, we also have ShoppingCartIcon and CartTotalText widgets that need to rebuild when the shopping cart data changes.
The CartService class acts as a middle man between the controllers and the repositories.
This is the perfect place to write logic that:
For more details, read this:
4 comments