LevitScope class

A hierarchical dependency injection container.

LevitScope manages the lifecycle of dependencies, supports hierarchical scoping, and allows for deterministic cleanup.

Resolution Rules

  1. Local Check: Searches the current scope's registry first.
  2. Parent Delegation: If not found locally, recursively searches parent scopes.
  3. Isolation: Dependencies registered in a child scope are not visible to parents.

Lifecycle

When a scope is disposed via dispose:

Available extensions

Constructors

LevitScope.root([String? name])
Creates a new root LevitScope.
factory

Properties

disposed Future<void>
Completes when terminal disposal completes, or immediately while active.
no setter
hashCode int
The hash code for this object.
no setterinherited
id int
The unique identifier for this scope.
final
isClosing bool
Whether terminal scope disposal has started.
no setter
isDisposed bool
Whether terminal scope disposal has completed.
no setter
name String
A descriptive name for debugging.
final
registeredCount int
The number of dependencies registered locally in this scope.
no setter
registeredKeys List<String>
A list of keys for all locally registered dependencies (for debugging).
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

bindExisting<Alias, Concrete extends Alias>({String? sourceTag, String? tag}) → void
Binds Alias to an existing local singleton registration of Concrete.
createScope(String name) LevitScope
Creates a new child scope.
delete<S>({String? tag, bool force = false}) Future<bool>
Removes a dependency from the scope.
dispose() Future<void>
Disposes the scope and all its dependencies.
find<S>({String? tag}) → S
Finds and returns a registered instance of type S.
findAsync<S>({String? tag}) Future<S>
Finds and returns an asynchronously registered instance of type S.
findOrNull<S>({String? tag}) → S?
Finds and returns a registered instance of type S, or null if not found.
findOrNullAsync<S>({String? tag}) Future<S?>
Asynchronously finds an instance of type S, or returns null if not found.
isInstantiated<S>({String? tag}) bool
Returns true if the dependency S has already been instantiated.
isRegistered<S>({String? tag}) bool
Returns true if type S is registered in this scope or any ancestor.
isRegisteredLocally<S>({String? tag}) bool
Returns true if type S is registered in this scope (parent scopes are ignored).
lazyPut<S>(S builder(), {String? tag, bool permanent = false, bool isFactory = false}) → void
Registers a dependency to be lazily instantiated.
lazyPutAsync<S>(Future<S> builder(), {String? tag, bool permanent = false, bool isFactory = false}) Future<S> Function()
Registers an asynchronous dependency to be lazily instantiated.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
put<S>(S builder(), {String? tag, bool permanent = false}) → S
Immediately instantiates and registers a dependency.
reset({bool force = false}) Future<void>
Disposes all dependencies registered in this scope.
run<R>(R callback()) → R

Available on LevitScope, provided by the LevitScopeImplicitScopeExtension extension

Executes callback with this scope as the active Ls.currentScope.
toString() String
A string representation of this object.
override

Operators

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

Static Properties

hasMiddlewares bool
no setter

Static Methods

addMiddleware(LevitScopeMiddleware middleware, {Object? token}) → void
Adds a global middleware to be notified of DI events.
containsMiddleware(LevitScopeMiddleware middleware) bool
containsMiddlewareToken(Object token) bool
removeMiddleware(LevitScopeMiddleware middleware) → void
Removes a previously added middleware.
removeMiddlewareByToken(Object token) bool
Removes a middleware by token.