The Get.lazyReplace Method
Explore how to effectively replace dependencies lazily in Flutter using Get.lazyReplace. Understand conditional initialization, tagged instance replacement, and dependency reloading with practical examples building increment and decrement controllers.
Replacing dependencies lazily
As the name suggests, Get.lazyReplace replaces the dependencies lazily, i.e., only when Get.find is called for the first time. Implementation-wise, it calls Get.lazyPut internally, unlike Get.replace, which calls Get.put internally. This means that the original dependency is deleted from the memory immediately, but the new dependency is injected only when needed.
Features of Get.lazyReplace
Get.lazyReplace comes bundled with three features that offer flexibility in replacing dependencies according to specific needs. The following are the features:
Conditionally replacing dependencies
Replacing specific instances
Reloading dependencies
Let us discuss each of them one by one.
Conditionally replacing dependencies
There are times when we want to initialize dependencies based on a condition. For example, we might choose between login and sign-up controllers based on whether the user is on the login or the sign-up page.
Get.lazyReplace follows the builder pattern which lets us initialize ...