Dependency Injection in Flutter with Injectable and GetIt

Manually registering dependencies in GetIt is fine for small apps, but it quickly becomes tedious and error-prone in larger projects. That’s where Injectable comes in—a code generation tool that integrates with GetIt and automates your Flutter dependency injection setup.

In this guide, you’ll learn how to use Injectable with GetIt to create cleaner, scalable, and boilerplate-free DI in your Flutter app. Mastering Flutter dependency injection will significantly improve your development workflow.

What is Injectable?

Injectable is a package that works with GetIt to generate the boilerplate code needed for dependency injection. Instead of registering your services manually, you annotate them with special markers, and Injectable takes care of the rest, making your Flutter dependency injection straightforward.

Why Use Injectable?

  • Reduces manual registration code
  • Improves scalability and maintainability
  • Easy to switch between singleton, lazy singleton, or factory
  • Compatible with Clean Architecture
  • Works seamlessly with test environments

Step 1: Add Dependencies

In your pubspec.yaml, add the following:

dependencies:
  get_it: ^7.6.4
  injectable: ^2.3.2

dev_dependencies:
  build_runner: ^2.4.6
  injectable_generator: ^2.4.0

Then run:

flutter pub get

Step 2: Setup GetIt and Injectable

Create a file called service_locator.dart:

import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'service_locator.config.dart';

final getIt = GetIt.instance;

@InjectableInit()
void configureDependencies() => getIt.init();

Run this command to generate the config file:

flutter pub run build_runner build --delete-conflicting-outputs

Step 3: Annotate Your Services

Create a class you want to inject, like UserService:

@injectable
class UserService {
  String getUserName() => 'John Doe';
}

Injectable will now register this automatically for you.

If you want it to be a singleton:

@singleton
class AuthService {
  bool isLoggedIn = false;
}

Step 4: Initialize in main.dart

In your main.dart, add the initialization code:

void main() {
  configureDependencies();
  runApp(MyApp());
}

Step 5: Access Dependencies

Anywhere in your app, retrieve a registered service:

final userService = getIt();
print(userService.getUserName());

No manual registration needed—Injectable handles it all, making Flutter dependency injection effortless.

Bonus: Use Modules for External Dependencies

Need to inject third-party services like Dio or SharedPreferences? You can use modules to include these in your Flutter dependency injection setup.

@module
abstract class RegisterModule {
  @lazySingleton
  Dio get dio => Dio();
}

When to Use Injectable

Use Injectable when:

  • Your app uses more than a few services
  • You want to follow Clean Architecture or MVVM
  • You’re tired of manually registering classes in GetIt
  • You want your DI to scale as your app grows

Conclusion

Combining Injectable with GetIt gives you the power of dependency injection without the headache of manual setup. With just a few annotations and code generation, you can build apps that are easier to test, maintain, and scale with effective Flutter dependency injection.

Start using Injectable in your Flutter project today—you’ll save time and write cleaner code while enjoying efficient Flutter dependency injection.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top