Skip to content
pubscout

injectable

3.0.0, released 7 months ago by codeness.ly

Convenient code generator for get_it dependency injection.

WASM-ready
$ flutter pub add injectable
Source repository

What it does

Injectable automates the wiring and registration of services for get_it using annotations like @injectable, @singleton, and @module. It analyzes your classes and generates an initialization extension method for your GetIt instance.

AI-written summary of version 3.0.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You are using get_it for dependency injection in your Flutter or Dart application
  • You want to eliminate boilerplate code for registering factories and singletons

Skip it when

  • You prefer manual service registration without code generation tools
  • You are not using get_it

Strengths

  • Reduces boilerplate associated with get_it registrations
  • Supports asynchronous initialization, singletons, factories, and factory parameters
  • Supports environments and named registrations

Weaknesses

  • Requires running a code generator (build_runner or lean_builder)
  • Adds an extra build step during development

Setup

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

Add injectable and get_it to dependencies, and injectable_generator and build_runner (or lean_builder) to dev_dependencies. Create a configuration function annotated with @InjectableInit and run the generator.

import '<FILE_NAME>.config.dart';
	
final getIt = GetIt.instance;  
  
@InjectableInit()  
void configureDependencies() => getIt.init();  

void main() {  
 configureDependencies();  
 runApp(MyApp());  
}

Alternatives

Packages that do the same job and could replace injectable.

Works with

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