LevitScope class
A hierarchical dependency injection container.
LevitScope manages the lifecycle of dependencies, supports hierarchical scoping, and allows for deterministic cleanup.
Resolution Rules
- Local Check: Searches the current scope's registry first.
- Parent Delegation: If not found locally, recursively searches parent scopes.
- Isolation: Dependencies registered in a child scope are not visible to parents.
Lifecycle
When a scope is disposed via dispose:
- All locally registered dependencies implementing LevitScopeDisposable will receive a LevitScopeDisposable.onClose callback.
- The scope becomes unusable and should be discarded.
- 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
Aliasto an existing local singleton registration ofConcrete. -
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, ornullif not found. -
findOrNullAsync<
S> ({String? tag}) → Future< S?> -
Asynchronously finds an instance of type
S, or returnsnullif not found. -
isInstantiated<
S> ({String? tag}) → bool -
Returns
trueif the dependencyShas already been instantiated. -
isRegistered<
S> ({String? tag}) → bool -
Returns
trueif typeSis registered in this scope or any ancestor. -
isRegisteredLocally<
S> ({String? tag}) → bool -
Returns
trueif typeSis 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
Executescallbackwith 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.