Sub topic 8
Registering and resolving with get_it
This chapter assumes you know Dart classes, abstract classes, implements, Futures, async and await, and basic Flutter state management. It also assumes that you have seen constructor injection and understand that a dependency is an object another class needs.
get_it is a service locator package for Dart and Flutter. You register objects with a central GetIt instance, then request them by type. This keeps object creation in one place, while feature classes receive the objects they need.
Create a service locator
Add get_it to pubspec.yaml:
dependencies:
get_it: ^8.0.0Create one locator for the application:
import 'package:get_it/get_it.dart';
final getIt = GetIt.instance;The GetIt instance stores registrations. A registration connects a type, such as UserRepository, to the function or object used to create it.
flowchart LR
A[setupLocator] --> B[Register UserRepository]
A --> C[Register ProfileViewModel]
D[Screen] --> E[getIt ProfileViewModel]
E --> C
C --> BRegister dependencies
Start with an abstraction and an implementation:
abstract class UserRepository {
Future<String> fetchName(int id);
}
class ApiUserRepository implements UserRepository {
@override
Future<String> fetchName(int id) async {
return 'User $id';
}
}
class ProfileViewModel {
ProfileViewModel(this._repository);
final UserRepository _repository;
String? name;
Future<void> load(int id) async {
name = await _repository.fetchName(id);
}
}Register both types in a setup function:
void setupLocator() {
getIt.registerLazySingleton<UserRepository>(
() => ApiUserRepository(),
);
getIt.registerFactory<ProfileViewModel>(
() => ProfileViewModel(getIt<UserRepository>()),
);
}registerLazySingleton delays creation until code first requests UserRepository. After that, the same repository instance is returned.
registerFactory runs its creator every time code requests ProfileViewModel. Each view model receives the shared repository.
Call setupLocator before the widget tree needs these objects:
void main() {
setupLocator();
runApp(const MyApp());
}Resolve a registered object by calling getIt<Type>():
final viewModel = getIt<ProfileViewModel>();
await viewModel.load(42);
print(viewModel.name); // User 42The registration type matters. If you register UserRepository, request UserRepository, not ApiUserRepository.
Choose a lifetime
A dependency lifetime describes how long an instance remains available.
| Registration | Creation time | Later requests |
|---|---|---|
| `registerSingleton` | Immediately during registration | Return the same instance |
| `registerLazySingleton` | On first request | Return the same instance |
| `registerFactory` | On every request | Return a new instance |
Use registerSingleton for an object that must be ready during startup. Use registerLazySingleton for shared services that may not be needed immediately. Use registerFactory for short-lived objects such as view models.
Avoid hidden dependencies
A service locator can make a class silently depend on global state:
class ProfileViewModel {
Future<void> load(int id) async {
final repository = getIt<UserRepository>();
// ...
}
}This is easy to write but harder to test because the dependency is hidden. Prefer constructor injection:
class ProfileViewModel {
ProfileViewModel(this._repository);
final UserRepository _repository;
}The locator should assemble objects. The objects themselves should receive their dependencies through constructors.
Exercise
Which registration should you use when every request must create a new ProfileViewModel, while all view models share one UserRepository?
Show answer
Use registerFactory<ProfileViewModel> and registerLazySingleton<UserRepository>.