What is Get.find?
Get.find is the method used to retrieve an already registered dependency in GetX. It looks up the instance by its type (and optionally a tag) and returns it. This allows you to access controllers, services, or any registered class from anywhere in your app without needing a BuildContext.
Real-World Use Cases
- Accessing a controller from another controller – e.g., a
CartControllerneeds to know the current user fromAuthController. - Calling controller methods from global helpers – e.g., from a custom
NotificationServiceto update UI. - Retrieving services in a
GetxService– e.g., aLoggerServiceneeds theAnalyticsService.
Basic Syntax
Using Get.find
Once you have registered a dependency using Get.put, Get.lazyPut, or Get.putAsync, you can retrieve it with Get.find.
Retrieving Without Storing
You don't have to store the result in a variable if you only need to call a method or access a property.
Using Tags
If you registered multiple instances of the same type using different tags, you must specify the tag in Get.find.
Checking if a Dependency is Registered
Before calling Get.find, you can check if the dependency exists using Get.isRegistered to avoid runtime errors.
Find vs GetView vs GetX Widget
| Method | Where to Use | Pros | Cons |
|---|---|---|---|
| `Get.find<T>()` | Anywhere (controller, service, widget) | Universal access, no boilerplate | Needs manual registration check |
| `GetView<T>` | In a StatelessWidget | Auto-provided `controller`, typed | Only works in widgets |
| `GetX<T>` | In widget build | Fine-grained rebuild control | More code than GetView |
In widgets, GetView is often preferred over Get.find because it gives you a typed controller without extra code. Use Get.find inside controllers, services, or when you need to access a dependency from a non‑widget context.
Find with LazyPut
When you use Get.lazyPut, the instance is created exactly when you call Get.find for the first time.
Performance Note
Get.find is extremely fast (it uses a hash map lookup). However, if you call it many times inside a build method, consider storing the result in a local variable to avoid repeated lookups – though this is rarely a performance bottleneck.
Best Practices
- Use
GetViewin widgets – It automatically provides a typedcontrollerwithout manualGet.find. - Check
isRegisteredbefore find – Especially in scenarios where the dependency may not be guaranteed to exist. - Store the result in a local variable if used multiple times – Avoid repeated
Get.findcalls for performance (though it's already fast). - Use tags for multiple instances – This keeps your DI organized and prevents accidental overwrites.
- Avoid using
Get.findinonCloseof the same controller – May cause infinite loops; instead pass references via constructor. - In services, prefer constructor injection – Let GetX inject dependencies via
Get.findwhen youGet.putthem, but keep constructors clear.
Common Mistakes
- ❌ Calling
Get.findbefore registration – Throws an exception. ✅ Always register first, or check withGet.isRegistered. - ❌ Forgetting tags when multiple instances exist – Returns the wrong instance or throws if ambiguous. ✅ Always specify the tag if you registered with one.
- ❌ Using
Get.findinside a build method withoutGetView– Works, butGetViewis cleaner and ensures the controller is available. - ❌ Assuming
Get.findcreates the instance – It only retrieves. UseGet.putorGet.lazyPutto register first.
FAQ
- Q: What happens if I call
Get.findfor a type that was never registered?
A: It throws an exception. Always ensure registration before usingGet.find. - Q: Can I use
Get.findin a background isolate?
A: No, GetX dependency injection is tied to the main isolate. Use standard Flutter communication (e.g.,sendPort) between isolates. - Q: Does
Get.findwork after the controller is disposed?
A: If the controller was disposed and not markedfenix: true,Get.findwill fail. CheckisRegisteredor usefenixto allow recreation. - Q: Can I use
Get.findinside a service?
A: Yes, as long as the dependency you're looking for is registered. This allows services to access controllers or other services. - Q: Is
Get.findthread‑safe?
A: Yes, within the Flutter UI thread. Do not use it from other threads.
Conclusion
Get.find is a simple yet powerful method to access registered dependencies anywhere in your Flutter app. Combined with Get.put, Get.lazyPut, and tags, it forms the backbone of GetX's dependency injection system. By following best practices, you can keep your code clean, testable, and efficient.