Container class

A hierarchical Dependency Injection (DI) container.

The Container supports singletons, factories, and scoped instances. It can be nested to create isolated scopes (e.g., per-request).

Available extensions

Constructors

Container({Container? parent})
Creates a new Container, optionally delegating to a parent.

Properties

hashCode int
The hash code for this object.
no setterinherited
parent Container?
The parent container to delegate to if a service is not found locally.
final
queue Queue

Available on Container, provided by the QueueContainerExtensions extension

Retrieves the registered Queue instance.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

child() Container
Creates a child container that delegates to this one for missing services.
dispose() Future<void>
Disposes of all locally registered instances that implement Disposable.
factory<T>(T factory(Container), {String? name}) → void
Registers a factory that will be executed every time resolve is called.
has<T>({String? name}) bool
Returns true if a service of type T is registered.
instance<T>(T instance, {String? name}) → void
Registers an instance locally in this container.
lazySingleton<T>(T factory(Container), {String? name}) → void
Registers a factory that will be executed once and its result stored as a singleton.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerInstance<T>(T instance, {String? name}) → void
Registers an instance locally in this container. Alias for instance.
resolve<T>({String? name}) → T
Resolves an instance of type T.
scoped<T>(T factory(Container), {String? name}) → void
Registers a factory that will be executed once per container scope (e.g. per request).
singleton<T>(T instance, {String? name}) → void
Registers a instance as a singleton available globally (or at this container's level).
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited