Sobes.tech
Middle

What dependency injection tools do you use in production?

sobes.tech AI

Answer from AI

Most often I use GetIt in combination with injectable.

GetIt is a simple Service Locator that easily integrates with other frameworks. It provides standard methods for registering services:

// Registering a singleton
GetIt.instance.registerSingleton<ApiService>(ApiService());

// Registering a factory
GetIt.instance.registerFactory<UserRepository>(() => UserRepository());

injectable is a code generator that allows annotating classes and generating code for GetIt, significantly simplifying the dependency injection process:

// Example of annotating a class for GetIt with injectable
@lazySingleton
class ApiService {}

// Command to generate GetIt code
// flutter pub run build_runner build

Combining these tools provides the benefits of both Service Locator (from GetIt) and Dependency Injection (thanks to automatic code generation with injectable), making DI in the project more manageable and less error-prone.

Sometimes I also used Provider for simple cases or in projects where state management is already based on Provider, as it can also serve for DI. However, for larger enterprise applications, I prefer the GetIt/injectable combo.