Dependency Injection with GetIt in Flutter app

NOVASOLUTIONS.TECHNOLOGY is engaged in the development, support and maintenance of iOS, Android, PWA mobile applications. We have extensive experience and expertise in publishing mobile applications in popular markets like Google Play, App Store, Amazon, AppGallery and others.
Development and support of all types of mobile applications:
Information and entertainment mobile applications
News apps, games, reference guides, online catalogs, weather apps, fitness and health apps, travel apps, educational apps, social networks and messengers, quizzes, blogs and podcasts, forums, aggregators
E-commerce mobile applications
Online stores, B2B apps, marketplaces, online exchanges, cashback services, exchanges, dropshipping platforms, loyalty programs, food and goods delivery, payment systems.
Business process management mobile applications
CRM systems, ERP systems, project management, sales team tools, financial management, production management, logistics and delivery management, HR management, data monitoring systems
Electronic services mobile applications
Classified ads platforms, online schools, online cinemas, electronic service platforms, cashback platforms, video hosting, thematic portals, online booking and scheduling platforms, online trading platforms

These are just some of the types of mobile applications we work with, and each of them may have its own specific features and functionality, tailored to the specific needs and goals of the client.

Showing 1 of 1 servicesAll 1735 services
Dependency Injection with GetIt in Flutter app
Simple
from 1 business day to 3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_mobile-applications_feedme_467_0.webp
    Development of a mobile application for FEEDME
    756
  • image_mobile-applications_xoomer_471_0.webp
    Development of a mobile application for XOOMER
    624
  • image_mobile-applications_rhl_428_0.webp
    Development of a mobile application for RHL
    1052
  • image_mobile-applications_zippy_411_0.webp
    Development of a mobile application for ZIPPY
    947
  • image_mobile-applications_affhome_429_0.webp
    Development of a mobile application for Affhome
    862
  • image_mobile-applications_flavors_409_0.webp
    Development of a mobile application for the FLAVORS company
    445

Dependency Injection Setup (GetIt) in Flutter App

GetIt is a service locator for Dart/Flutter, de facto standard for DI in projects that don't need code generation. Principle is simple: register dependencies once at app start, request anywhere via GetIt.instance<T>() or shorthand sl<T>().

Why GetIt Instead of Something Else

provider and Riverpod are State Management with DI as side effect. GetIt is pure service locator without Flutter dependencies: can use it in domain layer without BuildContext. For Clean Architecture where domain layer knows nothing about Flutter, this is principle.

GetIt constructor supports three registration modes:

  • registerSingleton<T> — creates immediately on registration, lives throughout app lifetime
  • registerLazySingleton<T> — creates on first access, then returns same instance
  • registerFactory<T> — creates new instance on every access

In practice: ApiService, DatabaseHelper, SharedPreferences wrapper — registerLazySingleton. ViewModel or BLoC if created by GetIt (rarely) — registerFactory. Usually BLoC created via BlocProvider, GetIt holds only infrastructure layer.

Common Initialization Mistake

// Wrong — synchronous registration of async dependency
sl.registerLazySingleton<DatabaseHelper>(() => DatabaseHelper()..init());

// Correct — async init via registerSingletonAsync
sl.registerSingletonAsync<DatabaseHelper>(() async {
  final db = DatabaseHelper();
  await db.init();
  return db;
});
// And wait for readiness before runApp:
await sl.allReady();

Without registerSingletonAsync + allReady(), DatabaseHelper may be requested before async initialization completes — crash on startup with StateError: Singleton is not ready yet.

Organizing injection_container.dart

Standard practice — one file injection_container.dart (or di/) with initDependencies() function:

Future<void> initDependencies() async {
  // External
  final sharedPrefs = await SharedPreferences.getInstance();
  sl.registerLazySingleton(() => sharedPrefs);
  sl.registerLazySingleton(() => http.Client());

  // Data sources
  sl.registerLazySingleton<AuthRemoteDataSource>(
    () => AuthRemoteDataSourceImpl(sl()),
  );

  // Repositories
  sl.registerLazySingleton<AuthRepository>(
    () => AuthRepositoryImpl(sl()),
  );

  // Use cases
  sl.registerLazySingleton(() => LoginUseCase(sl()));

  // BLoCs — if created via GetIt
  sl.registerFactory(() => AuthBloc(loginUseCase: sl()));
}

Register in dependency order bottom-up: first external deps, then data sources, repositories, use cases, finally — presentation layer.

GetIt + flutter_modular or Feature-Based DI

On large projects single injection_container.dart becomes 500 lines. Solution: split by features. Each feature module registers own deps via separate function initAuthDependencies(), initProfileDependencies() — called from main initDependencies().

What's Included in Setup

Analyze current project architecture → identify layers and dependencies → configure injection_container with correct lifetimes → setup async initialization for DB and Storage → cover registration with unit tests via mock substitution in test setUp.

Work takes 1–2 days depending on project size and number of existing dependencies.