CobaltInit class

Registers the annotated class as an async singleton whose init() is awaited during scope.init().

The class must declare Future<void> init() — implementing AsyncInitializable is the intended way. The generated factory constructs the object, awaits init(), and only then hands it to the scope, so anyone resolving it afterwards gets a fully initialized instance. Resolving it before scope.init() completes throws CobaltNotReadyError rather than returning a half-built object.

dependsOn turns the initializers into a graph. It is topologically sorted at build time into levels: everything in one level runs together through Future.wait, and the next level starts only once the previous finished. Declaring a cycle fails the build and names it.

@CobaltInit(dependsOn: [Database])
class SearchIndex implements AsyncInitializable {
  SearchIndex(this._database);
  final Database _database;

  @override
  Future<void> init() async => _database.buildIndex();
}
Annotations
  • @Target.new({TargetKind.classType})

Constructors

CobaltInit({List<Type> dependsOn = const <Type>[], bool lazy = false, Type? scope})
Creates an annotation marking an async initializer.
const

Properties

dependsOn → List<Type>
Types whose initialization must complete before this one starts.
final
hashCode → int
The hash code for this object.
no setterinherited
lazy → bool
Builds on the first getAsync instead of during scope.init().
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
scope → Type?
The @CobaltChildScope marker of the scope this is registered in, or null for the root. Its init() runs when that scope is initialized, as CobaltScope.open and CobaltScopeWidget do.
final

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