Dependency Injection in Flutter: Mastering GetIt and Injectable

Ranveer Singh Gour
3 min readJan 21, 2025

Dependency Injection (DI) is a key principle in building scalable, testable, and maintainable applications. In Flutter, the combination of GetIt and Injectable offers a powerful, developer-friendly approach to managing dependencies.

In this blog, we’ll explore how to use GetIt as a service locator and Injectable to automate the setup of dependencies. By the end, you’ll have a solid understanding of how to leverage these tools to streamline your Flutter projects.

Why Dependency Injection?

DI allows you to:

1. Decouple Components: Separate object creation from usage.

2. Improve Testability: Easily mock dependencies during testing.

3. Enhance Maintainability: Simplify code by reducing hard-coded dependencies.

What is GetIt?

GetIt is a simple and efficient service locator for Dart and Flutter. It helps manage dependencies without relying on complex frameworks.

What is Injectable?

Injectable is a code generator that works with GetIt. It simplifies dependency registration by generating boilerplate code for you.

Setting Up GetIt and Injectable

Here’s how you can integrate GetIt and Injectable into your Flutter project.

Step 1: Add Dependencies

Add the following dependencies to your pubspec.yaml:

dependencies:
get_it: ^latest_version
injectable: ^latest_version

dev_dependencies:
injectable_generator: ^latest_version
build_runner: ^latest_version

Step 2: Create a Service Locator

Create a file named service_locator.dart to initialize GetIt:

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

final GetIt getIt = GetIt.instance;

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

Step 3: Annotate Dependencies

Use Injectable annotations to mark classes for dependency injection.

Example: A Simple Service

import 'package:injectable/injectable.dart';

@lazySingleton
class ApiService {
void fetchData() {
print("Fetching data from API...");
}
}

Example: A ViewModel

import 'package:injectable/injectable.dart';
import 'api_service.dart';

@injectable
class HomeViewModel {
final ApiService apiService;

HomeViewModel(this.apiService);

void loadData() {
apiService.fetchData();
}
}

Step 4: Generate Dependency Configuration

Run the following command to generate the configuration file:

flutter pub run build_runner build

Step 5: Initialize Dependencies

Call configureDependencies() in your app’s entry point:

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

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

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

Using Dependencies

You can now retrieve and use dependencies anywhere in your app:

import 'package:flutter/material.dart';
import 'service_locator.dart';
import 'home_view_model.dart';

class HomeScreen extends StatelessWidget {
final HomeViewModel viewModel = getIt<HomeViewModel>();

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("GetIt & Injectable")),
body: Center(
child: ElevatedButton(
onPressed: viewModel.loadData,
child: Text("Fetch Data"),
),
),
);
}
}

Advanced Features

1. Scopes: Manage dependencies within specific scopes.

2. Named Registrations: Register multiple instances of the same type.

3. Environment-Specific Configurations: Use @Environment to manage dependencies for different environments (e.g., dev, prod).

Benefits of Using GetIt with Injectable

  • Automation: Injectable generates boilerplate code, reducing manual work.
  • Readability: Code becomes cleaner and easier to understand.
  • Flexibility: Easily swap or mock dependencies for testing.

Conclusion

Combining GetIt and Injectable is a game-changer for dependency management in Flutter. It not only simplifies the setup but also ensures your code remains modular, testable, and maintainable.

Start integrating GetIt and Injectable in your projects today, and experience the power of seamless dependency injection!

About the Author:

I am passionate Flutter developer with a love for creating beautiful and functional mobile applications. Connect with me on

Github: https://github.com/RanveerSingh1997 and LinkedIn:https://www.linkedin.com/in/ranveer-singh-gour/

--

--

Ranveer Singh Gour
Ranveer Singh Gour

Written by Ranveer Singh Gour

👨‍💻 Software Engineer | 🚀 Flutter, Android, KMM, Jetpack Compose | 🧠 Building Scalable Apps with Clean Architecture | 📢 Sharing Tech Insights

No responses yet