What is the difference between registerSingleton and registerFactory?
Singleton: same instance reused. Factory: new instance created on every getIt<T>() call.
Use get_it and injectable for scalable dependency injection
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Dependency Injection (DI) is a design pattern where objects receive their dependencies from the outside instead of creating them internally. Without DI, a class would create its own ApiService, DatabaseService, and other dependencies, making it impossible to test in isolation. With DI, you pass those dependencies in through the constructor, and a service locator or container manages the creation and lifetime of all objects. The most popular DI solution in Flutter is get_it, a simple service locator that lets you register and retrieve dependencies from anywhere in your app. For larger projects, the injectable package can auto-generate all registrations using code generation.
1// pubspec.yaml: get_it: ^7.6.02import 'package:get_it/get_it.dart';34final getIt = GetIt.instance;56// ── Register dependencies ──7void setupDependencies() {8// Singleton (one instance forever)9getIt.registerSingleton<ApiService>(ApiService());1011// Lazy Singleton (created on first use)12getIt.registerLazySingleton<DatabaseService>(() => DatabaseService());1314// Factory (new instance every time)15getIt.registerFactory<ProductRepository>(16() => ProductRepository(getIt<ApiService>(), getIt<DatabaseService>()),17);1819// Factory with ChangeNotifier20getIt.registerFactory<ProductViewModel>(21() => ProductViewModel(getIt<ProductRepository>()),22);23}2425// ── Initialize in main ──26void main() {27setupDependencies();28runApp(const MyApp());29}3031// ── Use anywhere ──32final vm = getIt<ProductViewModel>();33// or34final api = getIt<ApiService>();3536// ── In tests, you can replace implementations ──37getIt.unregister<ApiService>();38getIt.registerSingleton<ApiService>(MockApiService());
Without DI you must manually construct the entire dependency chain like ProductViewModel(ProductRepository(ApiService(), DatabaseService())). With DI you simply call getIt<ProductViewModel>() and the service locator builds the entire chain for you. DI manages the dependency graph automatically and makes swapping implementations for testing trivial.
DI containers are most maintainable when object lifetimes are explicit:
Correct scope planning prevents stale state, memory leaks, and hidden coupling.
Use get it and injectable for scalable dependency injection. This lesson adds one more building block for creating stable and user-friendly Flutter apps.
Think in architecture, reliability, and team-level maintainability.
After reading, rewrite the concept in your own words and apply it in a tiny runnable sample.
Each lesson becomes useful when you connect concept, implementation, and real-user outcome together.
Connect this lesson to separation of concerns so your codebase stays maintainable as it grows.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
Singleton: same instance reused. Factory: new instance created on every getIt<T>() call.
DI makes code testable (swap real services with mocks) and modular (change implementations without modifying dependents).
Dependencies should be registered once at app startup, before any widget tries to access them.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue