injectable_generator
3.1.3, released 3 months ago by codeness.ly
Code generator that creates GetIt registrations for the injectable library
What it does
It scans Dart files for @Injectable annotations and generates boilerplate code that registers services with GetIt. The generated code is type‑safe and supports advanced features like private named parameters and multiple registrations.
AI-written summary of version 3.1.3, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need a large or complex dependency graph and want to avoid manual registration boilerplate.
- You are already using injectable and want to keep the registration logic in a single place.
- You want compile‑time safety for your service registrations.
Skip it when
- You prefer manual, declarative registration or have a very small project where the extra build step is unnecessary.
- You are not using GetIt or injectable at all.
Strengths
- Reduces boilerplate and manual errors in service registration.
- Supports advanced features such as private named parameters and multiple registrations.
- Integrates with build_runner for fast incremental builds.
- Provides type safety and compile‑time checks.
Weaknesses
- Requires build_runner and a code‑generation step.
- Only works with the injectable package; not a general DI solution.
- Adds a dependency on the code‑generation tooling and can increase build times for very large projects.
Setup
- Kind
- Pure Dart
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 12
Add injectable_generator to dev_dependencies, run `flutter pub run build_runner build` or `flutter pub run build_runner watch` to generate code.
```dart
import 'package:injectable/injectable.dart';
@injectable
class ApiService {
ApiService();
}
@injectableInit
void configureDependencies() => $initGetIt(getIt);
```Alternatives
Packages that do the same job and could replace injectable_generator.
- inject_generator
Source code generator for compile-time dependency injection using annotations.
67Compare
Works with
Packages commonly used together with injectable_generator: ones it builds on, ones built on it, and its own family.
- analyzerinjectable_generator depends on itPerforms static analysis of Dart code for tool integration and embedding.
- globinjectable_generator depends on itA file and directory globbing library that supports Bash-style path matching and listing.
- dart_styleinjectable_generator depends on itProvides an API and CLI to format Dart source code automatically.
- code_builderinjectable_generator depends on itA fluent, builder-based library for generating valid Dart code programmatically.
- buildinjectable_generator depends on itPackage for authoring build_runner compatible code generators.
- source_geninjectable_generator depends on itSource code generation builders and utilities for the Dart build system.
- recaseinjectable_generator depends on itChanges the case of input text to various conventions.
- lean_builderinjectable_generator depends on itLean Builder: a fast, efficient Dart build system for code generation