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

  1. Immediate injection: Get.put not only initializes the dependency but also calls Get.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 use Get.put more often than other methods.

  2. Prevent deletion: Dependencies initialized with Get.put can be prevented from getting deleted if marked permanent. 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

  1. Lazy loading: Get.lazyPut loads the dependencies lazily. This means that they are created immediately, but injected only when Get.find is called manually. So, use Get.lazyPut when defining the dependencies in Bindings. This will inject the dependency only when it’s needed, preventing unnecessary memory allocation.

  2. 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.

  3. Reinitialize dependencies: Once deleted, the dependencies injected using Get.lazyPut can be reinitialized if we set its fenix property to true. This feature is handy when we’re unsure if the dependency will be needed again.

Choosing the Get.putAsync method

  1. Asynchronous initialization: Get.putAsync allows us to perform asynchronous operations before initializing the dependency. Ideal for initializing storage or API services.

  2. Prevent deletion: Get.putAsync can also be marked permanent, 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.

  3. 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

  1. Independent instances: Get.create creates a new instance of the dependency every time the dependency is accessed with Get.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.

  2. Prevents deletion by default: The permanent property of Get.create is set to true 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 using Get.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

  1. Lazy loading: Get.lazyReplace replaces the dependencies lazily. This means that the original dependency is deleted immediately, but the new one is injected only when Get.find is called manually. This is helpful to prevent unnecessary memory allocation, so use lazyReplace instead of replace unless the new instance is required immediately.

  2. 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.

  3. 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.