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

dart
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:

dart
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

dart
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

RegistrationBehaviorUse for
registerSingletonCreated immediately, one instanceThings needed at startup
registerLazySingletonCreated on first use, one instanceDio, repositories, databases
registerFactoryNew instance on every requestBLoCs, 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.

dart
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

dart
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

  1. Registering concrete types and depending on them. Depend on the abstraction (OrderRepository), register the implementation.
  2. Calling getIt inside domain code. It breaks the dependency rule from Clean Architecture.
  3. Forgetting reset() between tests, which leaks state across tests.
  4. 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 registerLazySingleton for infrastructure and registerFactory for BLoCs.
  • Keep get_it at 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.