Choosing the Right Injection
Learn to choose the ideal injection method based on the use case and scenario.
Why choosing the right injection is important
GetX offers quite several methods to inject dependencies. While each is fundamentally different and caters to a specific use case, we can still misinterpret the use cases in which a particular method should be used. Choosing the wrong injection method can hamper app performance and invite unwanted errors, so it’s important that we learn to make the right choice. This lesson acts as a guide that teaches us the function of each injection method and the use case it caters to.
Choosing the Get.put
method
Immediate injection:
Get.put
not only initializes the dependency but also callsGet.find
internally, making the dependency available immediately. So, use it when we want immediate access to the dependency. This is the most common scenario, and we’ll useGet.put
more often than other methods.Prevent deletion: Dependencies initialized with
Get.put
can be prevented from getting deleted if markedpermanent
. This can be handy when preserving the controller’s state for the entire app’s life cycle. Think of a settings controller.
Choosing the Get.lazyPut
method
Lazy loading:
Get.lazyPut
loads the dependencies lazily. This means that they are created immediately, but injected only whenGet.find
is called manually. So, useGet.lazyPut
when defining the dependencies in Bindings. This will inject the dependency only when it’s needed, preventing unnecessary memory allocation.Conditional injection: It implements a builder pattern to define the dependencies, so we can inject them conditionally. This is ideal for cases when we want to switch between login and signup controllers or inject an admin controller after checking for admin privileges.
Reinitialize dependencies: Once deleted, the dependencies injected using
Get.lazyPut
can be reinitialized if we set itsfenix
property to true. This feature is handy when we’re unsure if the dependency will be needed again.
Choosing the Get.putAsync
method
Asynchronous initialization:
Get.putAsync
allows us to perform asynchronous operations before initializing the dependency. Ideal for initializing storage or API services.Prevent deletion:
Get.putAsync
can also be markedpermanent
, and the dependencies can be prevented from deletion. This feature is essential when we talk about services, as they are needed for the entire life cycle of the app.Conditional injection: With its builder pattern, we can inject dependencies conditionally. This allows us to switch between databases or API environments.
Choosing the Get.create
method
Independent instances:
Get.create
creates a new instance of the dependency every time the dependency is accessed withGet.find
. This is useful if we want to maintain independent states for the widgets. Each widget connects to a new and unique instance of the controller and manages its state independently.Prevents deletion by default: The
permanent
property ofGet.create
is set totrue
by default. This means that the dependencies remain available for the entire life cycle.
Choosing the Get.replace
method
Replace instances: Dependencies injected by
Get.put
(or other methods) can be replaced usingGet.replace
. This feature is useful for inherited classes. For example, we can replace a controller with its child controller to switch the implementation details at any time.
Choosing the Get.lazyReplace
method
Lazy loading:
Get.lazyReplace
replaces the dependencies lazily. This means that the original dependency is deleted immediately, but the new one is injected only whenGet.find
is called manually. This is helpful to prevent unnecessary memory allocation, so uselazyReplace
instead ofreplace
unless the new instance is required immediately.Conditional injection: We have the builder pattern which lets us provide the new dependency conditionally. Again, this is helpful when we wish to switch between two controllers based on a condition or check for permissions before injecting an instance.
Reinitialize dependencies: We get the
fenix
property that reinitializes deleted dependencies if they are needed again. It is handy if we’re not sure if we will need the replaced dependency again in the app.
Conclusion
Every injection method is unique in terms of implementation and usage scenario. If we learn to use the right method for the use case, our apps will become safer and more performant. So, come back to this lesson when unsure of which method to use in a certain situation.
Get hands-on with 1400+ tech skills courses.