injectable
3.0.0, released 7 months ago by codeness.ly
Convenient code generator for get_it dependency injection.
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.
- inject_generator
Source code generator for compile-time dependency injection using annotations.
67Compare
Works with
Packages commonly used together with injectable: ones it builds on, ones built on it, and its own family.
- get_itinjectable depends on itA fast, type-safe service locator for Dart and Flutter without BuildContext or code generation.
- injectable_generatorBuilt on injectableCode generator that creates GetIt registrations for the injectable library
- network_toolsBuilt on injectableDiscover open ports, devices on a subnet, and perform network scanning.