inject_generator
1.2.1, released 3 months ago by dasralph.de
Source code generator for compile-time dependency injection using annotations.
What it does
Reads @inject, @component, and @module annotations to generate fully wired, type-safe dependency components at build time. It eliminates runtime reflection and global state, supporting constructor injection and Flutter view-model factories.
AI-written summary of version 1.2.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You want compile-time safety and validation for your dependencies
- You prefer explicit constructor injection over runtime service locators
- You are building a Flutter application that can benefit from build_runner code generation
Skip it when
- You cannot use build_runner or code generation in your workflow
- You prefer runtime service locators like get_it
Strengths
- Zero runtime overhead and no reflection (no dart:mirrors)
- Compile-time validation catches missing bindings and cycles early
- Strong Flutter integration via inject_flutter and ViewModelFactory
Weaknesses
- Requires running build_runner after annotation changes
- Generated code adds build artifacts to your project
Setup
- Kind
- Pure Dart
- Effort
- Medium
- Code generation
- Yes (build_runner)
- Direct dependencies
- 14
Add inject_annotation, dev dependency inject_generator, and build_runner to your pubspec.yaml, then run build_runner.
@inject
class CounterViewModel extends ChangeNotifier {
int count = 0;
void increment() {
count++;
notifyListeners();
}
}Alternatives
Packages that do the same job and could replace inject_generator.
- auto_injector
Automatic dependency injection system for Dart and Flutter without build_runner.
75Compare - lite_ref
A lightweight, no‑dependency DI library for Dart and Flutter.
84Compare - injector
A simple to use and lightweight dependency injection library for Dart.
50Compare - injectable
Convenient code generator for get_it dependency injection.
78Compare
Works with
Packages commonly used together with inject_generator: ones it builds on, ones built on it, and its own family.
- analyzerinject_generator depends on itPerforms static analysis of Dart code for tool integration and embedding.
- pub_semverinject_generator depends on itVersions and version constraints implementing pub's versioning policy.
- stack_traceinject_generator depends on itProvides parsing, inspection, and readable formatting of Dart stack traces.
- globinject_generator depends on itA file and directory globbing library that supports Bash-style path matching and listing.
- dart_styleinject_generator depends on itProvides an API and CLI to format Dart source code automatically.
- code_builderinject_generator depends on itA fluent, builder-based library for generating valid Dart code programmatically.
- logginginject_generator depends on itProvides configurable logging with levels and stream listeners for Dart apps.
- buildinject_generator depends on itPackage for authoring build_runner compatible code generators.