Skip to content
pubscout

inject_annotation

1.2.1, released 3 months ago by dasralph.de

Constructor-based compile-time dependency injection framework for Dart and Flutter.

WASM-ready
$ flutter pub add inject_annotation
Source repository

What it does

Provides compile-time dependency injection using annotations and code generation. It wires services explicitly via constructors and modules without relying on runtime reflection or global state.

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 validation for missing dependencies and cycles
  • You prefer explicit constructor-based dependency injection without runtime lookups
  • You are building a Flutter application that benefits from typed view model factories and zero reflection overhead

Skip it when

  • You prefer runtime dependency injection or service-locator patterns like get_it
  • You do not want to use code generation and build_runner in your workflow

Strengths

  • Compile-time validation catches wiring issues and cycles early
  • Zero runtime overhead and no reflection
  • Refactor-safe with strong Flutter and view model integration

Weaknesses

  • Requires running build_runner for code generation
  • Strictly enforces constructor injection

Setup

Kind
Pure Dart
Effort
Medium
Code generation
Yes (build_runner)
Direct dependencies
1

Add inject_annotation and build_runner with inject_generator as dev dependencies, 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_annotation.

  • injectable

    If you want a more annotation-friendly abstraction on top of get_it service locator rather than pure compile-time constructor injection.

  • get_it

    If you prefer a simpler runtime service locator instead of generating compile-time components.

Works with

Packages commonly used together with inject_annotation: ones it builds on, ones built on it, and its own family.