Dependency Injection (GetIt)
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
In the MoodTune application, Dependency Injection (DI) is managed using the get_it service locator. This system provides a centralized way to manage the lifecycle of various components across the application's clean architecture layers, ensuring that dependencies point inward from Presentation to Domain and finally to the Data layer.
The DI container is primarily responsible for initializing and providing singleton instances of core services, repositories, and use cases. It facilitates the decoupling of implementation details from business logic, allowing components to request what they need without knowing how those dependencies are constructed.
Sources: CLAUDE.md:61-63, authplan.md:329-338
Architecture and Configuration
The DI system is initialized during the application's bootstrap process. A global singleton instance of GetIt is defined, and a configuration function is called to register all necessary dependencies before the app starts.
Initialization Flow
The application initialization sequence ensures that the service locator is fully populated before any UI components attempt to access services.
The configureDependencies() function, typically located in lib/di/injector.dart, is responsible for this registration.
Sources: CLAUDE.md:38-41, authplan.md:296-297
Dependency Layers
Dependencies are registered according to their role in the Clean Architecture.
| Layer | Component Types | Description |
|---|---|---|
| Core | FlutterSecureStorage, ApiClient, NetworkInfo | Low-level infrastructure and external interface wrappers. |
| Data | SupabaseAuthService, AnalysisRemoteDataSource | Implementations of remote and local data access. |
| Domain | AuthRepository, AnalysisRepository | Abstract contracts and business logic use cases. |
Sources: authplan.md:329-338, CLAUDE.md:65-72
Component Registration
The project uses get_it version ^7.6.4. Registrations are typically categorized into core infrastructure and feature-specific dependencies.
Core Infrastructure
Core components like TokenStorage and ApiClient are registered first, as they are often required by higher-level data sources. The TokenStorage handles secure storage using FlutterSecureStorage and includes logic for handling large tokens via chunking.
Sources: authplan.md:73-77, authplan.md:91-103
Feature-Specific Dependencies
Each feature (Auth, Spotify, Analysis) has its repositories and data sources registered. For example, the Authentication feature registers:
SupabaseAuthService(Data Source)AuthRepository(Domain Implementation)- Various Use Cases (e.g.,
LoginUseCase,LogoutUseCase)
Sources: authplan.md:329-338, CLAUDE.md:75-80
Integration with Other Systems
Integration with BLoC
The Presentation layer utilizes flutter_bloc for state management. BLoCs retrieve their required use cases or repositories directly from the get_it instance. When providing BLoCs at the route level, the BlocProvider.value pattern is used in conjunction with the service locator (sl) to reuse existing instances.
Sources: routing_plan.md:126-135, CLAUDE.md:103-107
Integration with Routing
The AppRouter uses the service locator to access authentication services for route guarding. A refreshListenable is often wired to the SupabaseAuthService obtained from GetIt to trigger automatic redirects when auth states change.
Sources: routing_plan.md:200-210, routing_plan.md:465-475
Key Implementation Details
Token Provider Lambda
A specific pattern used in MoodTune involves a tokenProvider lambda. This lambda captures the Supabase session token at the time of the call and passes it to data sources for Bearer authentication, ensuring that data sources always use the most current session token.
Sources: CLAUDE.md:107-109
Authentication Interceptor
The AuthInterceptor is injected into the Dio HTTP client to handle token injection and automatic 401 unauthorized retries. It interacts with the TokenStorage (retrieved via DI) to manage the lifecycle of the access token.
Sources: authplan.md:129-141, CLAUDE.md:118-120
Summary
Dependency Injection via GetIt serves as the backbone for service management in the MoodTune app. By centralizing component instantiation, it enables a clean separation of concerns and facilitates the testing of individual layers by allowing easy mocking of dependencies. The system ensures that core services like authentication and network clients are available throughout the app while maintaining the integrity of the project's Clean Architecture.
Sources: authplan.md:387-393, CLAUDE.md:61-63