LevitScope class

A hierarchical dependency injection container for managing resource lifecycles.

LevitScope provides a registry for dependencies with support for nesting, deterministic cleanup, and override behavior.

Resolution Rules

  1. Local First: Searches the current scope's registry.
  2. Breadth-Up: If not found locally, recursively searches parent scopes.
  3. Isolation: Dependencies in child scopes are invisible to parents.

Cleanup

Disposing a scope via dispose automatically disposes all its managed dependencies that implement LevitScopeDisposable.

Available extensions

Constructors

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

Properties

hashCode int
The hash code for this object.
no setterinherited
id int
Unique identifier for this scope instance.
final
name String
The name of this scope, used for debugging purposes.
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

createScope(String name) LevitScope
Creates a new child scope branching from this scope.
delete<S>({String? tag, bool force = false}) bool
Removes a registration and disposes its instance.
dispose() → void
Disposes this scope and all its dependencies.
find<S>({String? tag}) → S
Retrieves the registered instance of type S.
findAsync<S>({String? tag}) Future<S>
Asynchronously resolves the registered instance of type S.
findOrNull<S>({String? tag}) → S?
Resolves the registered instance of type S, or null if not found.
findOrNullAsync<S>({String? tag}) Future<S?>
Asynchronously finds an instance of type S, returning null if not found.
isInstantiated<S>({String? tag}) bool
Whether type S has already been instantiated.
isRegistered<S>({String? tag}) bool
Returns true if type S is registered in this scope or any parent scope.
isRegisteredLocally<S>({String? tag}) bool
Returns true if type S is registered in this specific scope.
lazyPut<S>(S builder(), {String? tag, bool permanent = false, bool isFactory = false}) → void
Registers a lazy builder in this scope.
lazyPutAsync<S>(Future<S> builder(), {String? tag, bool permanent = false, bool isFactory = false}) Future<S> Function()
Registers an asynchronous lazy builder in this scope.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
put<S>(S builder(), {String? tag, bool permanent = false}) → S
Instantiates and registers a dependency instance in this scope.
reset({bool force = false}) → void
Deletes all non-permanent dependencies in this specific scope.
run<R>(R callback()) → R

Available on LevitScope, provided by the LevitScopeImplicitScopeExtension extension

Executes the callback within a Zone where this scope is active.
toString() String
A string representation of this object.
override

Operators

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

Static Properties

hasMiddlewares bool
Whether any middlewares are registered.
no setter

Static Methods

addMiddleware(LevitScopeMiddleware middleware) → void
Adds a global middleware to be notified of DI events.
removeMiddleware(LevitScopeMiddleware middleware) → void
Removes a previously added middleware.