Skip to content
pubscout

injectable_generator

3.1.3, released 3 months ago by codeness.ly

Code generator that creates GetIt registrations for the injectable library

$ flutter pub add injectable_generator
Source repository

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.

Works with

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