Injected class

Fills the annotated field from the scope instead of from a constructor.

The field must be late final, and the class must mix in the generated _$ClassName, which declares a setter for each annotated field and assigns them all in onInject. The scope calls onInject immediately after the constructor returns, so the fields are set before anyone can read them.

This is how a controller keeps an empty constructor no matter how many dependencies it has:

@cobaltTransient
class NotesController with _$NotesController {
  NotesController();

  @injected
  late final NoteStore _store;
}

late final still means write-once: a second assignment throws LateInitializationError. Because the generated mixin lands in a part file of the same library, private fields work.

Annotations
  • @Target.new({TargetKind.field})

Constructors

Injected({String? name})
Creates an annotation marking a field for property injection.
const

Properties

hashCode → int
The hash code for this object.
no setterinherited
name → String?
Resolves the named registration rather than the default one.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

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