What is Get.put?
Get.put is the primary method in GetX for immediate dependency injection. It creates an instance of a class (usually a controller or service) and makes it available throughout your app. Unlike Get.lazyPut, the instance is created right away, not lazily on first access. This is perfect for dependencies that you know will be needed soon, or for global services that must be ready immediately.
Basic Syntax
Returning the Instance
Get.put returns the instance you just created, so you can store it directly for later use. This is common in views to have a typed reference without needing Get.find.
Permanent Controllers
By default, controllers injected with Get.put are not permanent – they will be disposed when the route that created them is popped, unless you set permanent: true. Use this for global services that should live the entire app lifecycle.
Using Tags for Multiple Instances
If you need multiple instances of the same type, you can give each a unique tag. This allows you to differentiate them when using Get.find.
Injection with Bindings
In a binding, you can also use Get.put if you need the instance immediately. However, Get.lazyPut is often preferred for better startup performance.
Get.put vs Get.lazyPut
Get.put– Creates instance immediately. Use when the dependency is needed right away or you want the instance to be ready even if never accessed.Get.lazyPut– Creates instance only on firstGet.find. Use for better startup performance when the dependency might not be used immediately.
Get.put vs Get.create
Get.put– Creates a singleton instance. EachGet.findreturns the same instance.Get.create– Creates a new instance every timeGet.findis called. Useful for transient dependencies.
Checking Registration
Before using Get.find, you can check if an instance is already registered to avoid errors.
Best Practices
- Use
Get.putininitStateor bindings – Avoid placing it directly in thebuildmethod to prevent duplicate instances on rebuilds. - Prefer
Get.lazyPutfor most cases – It improves startup performance, especially for routes that may not be visited. - Use
permanent: truesparingly – Only for truly global dependencies (e.g., authentication service, theme manager). - Use tags when you need multiple instances – For example, in a list of items where each item has its own controller.
- Inject dependencies via constructor – Even with
Get.put, you can pass dependencies to the controller's constructor for better testability.
Common Mistakes
- ❌ Calling
Get.putinsidebuildmethod – Creates a new instance on every rebuild. ✅ Place it ininitStateof aStatefulWidget, or better, use bindings. - ❌ Forgetting to check
isRegisteredbeforeGet.put– Can accidentally overwrite existing instances. ✅ UseGet.isRegisteredor rely on the framework to ensure you don't duplicate. - ❌ Using
Get.putwhen you need multiple instances – Overwrites the previous one. ✅ Use tags to distinguish instances. - ❌ Not disposing resources in
onClose– Even with permanent controllers, you should clean up listeners.
FAQ
- Q: When should I use
Get.putvsGet.lazyPut?
A: UseGet.putif you need the instance immediately (e.g., in the initial binding of a route that will be accessed). UseGet.lazyPutfor most other cases to defer creation until needed. - Q: Can I use
Get.putinside a controller?
A: Yes, but be careful about circular dependencies. It's better to inject dependencies via the controller's constructor and register them in bindings. - Q: What happens if I call
Get.puttwice for the same type?
A: The second call will overwrite the first if no tag is used. UseGet.isRegisteredto check before re-registering. - Q: How do I remove an instance registered with
Get.put?
A: UseGet.delete<MyController>()(optionally with a tag). This will also call the controller'sonClose. - Q: Does
Get.putautomatically dispose the controller?
A: By default, it is tied to the route's lifecycle. When the route is popped, the controller is disposed unlesspermanent: trueis set.
Conclusion
Get.put is a simple yet powerful method for immediate dependency injection in GetX. It gives you fine control over when dependencies are created, and combined with tags and permanence, it handles everything from simple controllers to complex global services. When used judiciously, it contributes to clean, maintainable Flutter applications.