Skip to content
pubscout

inject_generator

1.2.1, released 3 months ago by dasralph.de

Source code generator for compile-time dependency injection using annotations.

$ flutter pub add inject_generator
Source repository

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.

  • lite_ref

    A lightweight, no‑dependency DI library for Dart and Flutter.

  • injector

    A simple to use and lightweight dependency injection library for Dart.

  • injectable

    Convenient code generator for get_it dependency injection.

Works with

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