Dependency injection (DI) sounds heavy, but the idea is small: a class should receive the things it needs instead of creating them. In Flutter, get_it is the most common way to wire those things together. This guide covers the concept, a clean setup, and the mistakes that turn DI into a mess.
The problem DI solves
class OrdersBloc extends Bloc<OrdersEvent, OrdersState> {
// Hard-wired: you cannot test this without a real network
final _repository = OrderRepositoryImpl(OrderApi(Dio()));
}
This class decides how its dependencies are built. You cannot swap in a fake, and every change to OrderApi's constructor breaks it. With constructor injection:
class OrdersBloc extends Bloc<OrdersEvent, OrdersState> {
OrdersBloc(this._repository) : super(const OrdersLoading());
final OrderRepository _repository;
}
Now something else is responsible for building it. That something is the composition root, and get_it is a convenient place to put it.
Setting up get_it
final getIt = GetIt.instance;
Future<void> configureDependencies() async {
// Infrastructure: one instance for the whole app
getIt.registerLazySingleton<Dio>(() => createDio());
getIt.registerLazySingleton<OrderApi>(() => OrderApi(getIt()));
// Abstractions mapped to implementations
getIt.registerLazySingleton<OrderRepository>(
() => OrderRepositoryImpl(getIt()),
);
getIt.registerLazySingleton(() => GetOrders(getIt()));
// State holders: a new instance each time
getIt.registerFactory(() => OrdersBloc(getIt()));
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await configureDependencies();
runApp(const App());
}
Choosing a lifetime
| Registration | Behavior | Use for |
|---|---|---|
registerSingleton | Created immediately, one instance | Things needed at startup |
registerLazySingleton | Created on first use, one instance | Dio, repositories, databases |
registerFactory | New instance on every request | BLoCs, cubits, screen-level state |
A common bug: registering a BLoC as a singleton. It then keeps the state of the last screen visit and is closed once while still registered.
Using it in the UI
Resolve at the edge of the widget tree and pass the object down; do not call getIt all over your widgets.
BlocProvider(
create: (_) => getIt<OrdersBloc>()..add(OrdersRequested()),
child: const OrdersView(),
)
Service locator vs real DI
getIt<T>() called inside a class is the service locator pattern: the class asks for what it needs. It works, but it hides dependencies and makes classes harder to test. Keep getIt at the composition root (registration and the widgets that create BLoCs). Inside business classes, use constructor injection. That gives you the convenience of get_it with the clarity of real DI.
Overriding in tests
setUp(() {
getIt.reset();
getIt.registerFactory<OrderRepository>(() => FakeOrderRepository());
getIt.registerFactory(() => OrdersBloc(getIt()));
});
Because the BLoC takes a repository in its constructor, most unit tests do not need get_it at all: construct the object with a fake directly. See testing Flutter apps for patterns.
Mistakes to avoid
- Registering concrete types and depending on them. Depend on the abstraction (
OrderRepository), register the implementation. - Calling
getItinside domain code. It breaks the dependency rule from Clean Architecture. - Forgetting
reset()between tests, which leaks state across tests. - Circular dependencies. If A needs B and B needs A, the design needs fixing, not the container.
If you want less manual registration, injectable generates get_it setup from annotations. It is a convenience layer; the concepts above are the same.
Key takeaways
- DI means classes receive their dependencies; they do not build them.
- Use
registerLazySingletonfor infrastructure andregisterFactoryfor BLoCs. - Keep
get_itat the composition root and use constructor injection inside classes. - DI is what makes the SOLID dependency inversion principle practical.
Need a Flutter engineer who cares about this? Let's talk.