flutter
/

GetX Get.find: Retrieving Dependencies Anywhere

Last Sync: Today

On this page

14
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

flutter

GetX Get.find: Retrieving Dependencies Anywhere

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 CartController needs to know the current user from AuthController.
  • Calling controller methods from global helpers – e.g., from a custom NotificationService to update UI.
  • Retrieving services in a GetxService – e.g., a LoggerService needs the AnalyticsService.

Basic Syntax

DARTRead-only
1
// Retrieve a controller registered earlier
final controller = Get.find<MyController>();

// Now use it
controller.someMethod();

Using Get.find

Once you have registered a dependency using Get.put, Get.lazyPut, or Get.putAsync, you can retrieve it with Get.find.

DARTRead-only
1
class HomeController extends GetxController { ... }

// Register (in a binding or initState)
Get.put(HomeController());

// Later, anywhere:
final controller = Get.find<HomeController>();

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.

DARTRead-only
1
// Direct method call
Get.find<HomeController>().increment();

// In a widget
Obx(() => Text(Get.find<HomeController>().count.toString()))

Using Tags

If you registered multiple instances of the same type using different tags, you must specify the tag in Get.find.

DARTRead-only
1
Get.put(UserController(), tag: 'alice');
Get.put(UserController(), tag: 'bob');

final alice = Get.find<UserController>(tag: 'alice');
final bob = Get.find<UserController>(tag: 'bob');

Checking if a Dependency is Registered

Before calling Get.find, you can check if the dependency exists using Get.isRegistered to avoid runtime errors.

DARTRead-only
1
if (Get.isRegistered<MyController>()) {
  final controller = Get.find<MyController>();
} else {
  // Register it or handle absence
}

Find vs GetView vs GetX Widget

MethodWhere to UseProsCons
`Get.find<T>()`Anywhere (controller, service, widget)Universal access, no boilerplateNeeds manual registration check
`GetView<T>`In a StatelessWidgetAuto-provided `controller`, typedOnly works in widgets
`GetX<T>`In widget buildFine-grained rebuild controlMore 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.

DARTRead-only
1
Get.lazyPut<HeavyService>(() => HeavyService());

// At this point, HeavyService is NOT created yet
// But when we call find, it will be instantiated
final service = Get.find<HeavyService>();

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 GetView in widgets – It automatically provides a typed controller without manual Get.find.
  • Check isRegistered before 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.find calls for performance (though it's already fast).
  • Use tags for multiple instances – This keeps your DI organized and prevents accidental overwrites.
  • Avoid using Get.find in onClose of the same controller – May cause infinite loops; instead pass references via constructor.
  • In services, prefer constructor injection – Let GetX inject dependencies via Get.find when you Get.put them, but keep constructors clear.

Common Mistakes

  • ❌ Calling Get.find before registration – Throws an exception. ✅ Always register first, or check with Get.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.find inside a build method without GetView – Works, but GetView is cleaner and ensures the controller is available.
  • ❌ Assuming Get.find creates the instance – It only retrieves. Use Get.put or Get.lazyPut to register first.

FAQ

  • Q: What happens if I call Get.find for a type that was never registered?
    A: It throws an exception. Always ensure registration before using Get.find.
  • Q: Can I use Get.find in 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.find work after the controller is disposed?
    A: If the controller was disposed and not marked fenix: true, Get.find will fail. Check isRegistered or use fenix to allow recreation.
  • Q: Can I use Get.find inside 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.find thread‑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.

Try it yourself

import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      home: HomePage(),
    );
  }
}

class CounterController extends GetxController {
  var count = 0.obs;
  void increment() => count++;
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Register the controller (could also be in a binding)
    Get.put(CounterController());

    return Scaffold(
      appBar: AppBar(title: Text('Get.find Demo')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // Using Get.find directly in Obx
            Obx(() => Text(
              'Count: ${Get.find<CounterController>().count}',
              style: TextStyle(fontSize: 32),
            )),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                // Retrieve and call method
                Get.find<CounterController>().increment();
              },
              child: Text('Increment'),
            ),
            ElevatedButton(
              onPressed: () {
                // Check if registered
                if (Get.isRegistered<CounterController>()) {
                  Get.snackbar('Info', 'Controller exists');
                } else {
                  Get.snackbar('Info', 'Controller not found');
                }
              },
              child: Text('Check Registration'),
            ),
          ],
        ),
      ),
    );
  }
}

Test Your Knowledge

Q1
of 3

What does Get.find do?

A
Registers a new dependency
B
Retrieves an existing registered dependency
C
Deletes a dependency
D
Creates a lazy dependency
Q2
of 3

How do you safely check if a dependency exists before calling Get.find?

A
Get.exists()
B
Get.isRegistered()
C
Get.has()
D
Get.available()
Q3
of 3

What is the purpose of the tag parameter in Get.find?

A
To set a name for debugging
B
To differentiate between multiple instances of the same type
C
To make the instance permanent
D
To lazy load the instance

Previous

getx lazyput

Next

getx snackbar

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.