Decoupling dependency creation from usage
The practice of decoupling dependencies is not specific to Flutter. There is a high chance that you have already encountered the concept of IoC, as well as specific patterns such as DI and SL. If you have, this chapter will highlight some Flutter-specific approaches, along with their pros and cons. If you haven’t, that’s not a problem. We will discuss them in detail now. However, before we delve into the terminology and provide a solution, we need to identify the problem. Let’s revisit CartModel
, which we created in Chapter 4. In Chapter 6, we refactored CartModel
to adhere to the repository pattern by renaming it to InMemoryCartRepository
and implementing the abstract CartRepository
. Besides doing this, we have also removed the static constructor. Now, let’s zoom in to understand why we did that.
Identifying the singleton pattern
The previous instantiation of CartModel
used a very specific approach and looked...