DependencyInjectionGenerator class
Generator that creates dependency injection methods for classes annotated with dependency injection annotations.
This generator automatically analyzes classes with annotations like @RegisterFactory, @RegisterSingleton, @RegisterLazySingleton, etc. and generates appropriate dependency injection methods that integrate with GetIt.
Supported Annotations
- RegisterFactory: Creates new instance each time
- RegisterSingleton: Creates instance immediately and reuses it
- RegisterLazySingleton: Creates instance on first use, then reuses it
- RegisterAsyncFactory: Creates new async instance each time
- RegisterAsyncSingleton: Creates async instance immediately and reuses it
- RegisterAsyncLazySingleton: Creates async instance on first use, then reuses it
Generated Output
For a class like:
@RegisterSingleton()
class MyService {
final Repository _repository;
final String _apiKey;
MyService(this._repository, [this._apiKey = 'default-key']);
}
It generates:
MyService getMyService({String apiKey = 'default-key'}) {
return GetIt.instance.getOrRegister<MyService>(
() => MyService(getRepository(), apiKey), RegisterAs.singleton);
}
For async classes:
@RegisterAsyncFactory()
class MyService {
final Repository _repository;
final String _apiKey;
MyService(this._repository, [this._apiKey = 'default-key']);
Future<void> doSomething() async {
// Your async service logic
}
}
It generates:
Future<MyService> getMyService({String apiKey = 'default-key'}) async {
return await GetIt.instance.getOrRegisterAsync<MyService>(
() async => MyService(getRepository(), apiKey), RegisterAs.factoryAsync);
}
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- inPackage → String?
-
Annotation package for
TypeChecker.typeNamed.finalinherited - inSdk → bool?
-
Annotation package type for
TypeChecker.typeNamed.finalinherited - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- throwOnUnresolved → bool
-
finalinherited
- typeChecker → TypeChecker
-
no setterinherited
Methods
-
generate(
LibraryReader library, BuildStep buildStep) → FutureOr< String> -
Generates Dart code for an input Dart library.
inherited
-
generateForAnnotatedDirective(
ElementDirective directive, ConstantReader annotation, BuildStep buildStep) → dynamic -
Implement to return source code to generate for
directive:inherited -
generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) → String -
Implement to return source code to generate for
element. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited