LevitController class abstract
A base class for business logic components with automated resource management and explicit lifecycle hooks.
LevitController provides a structured environment for managing the lifecycle of application logic. It implements LevitScopeDisposable to integrate with the Levit dependency injection system.
The primary responsibility of a controller is to encapsulate business logic and ensure that all resources (streams, timers, reactive variables) are cleaned up when the controller is removed from its scope.
Lifecycle Lifecycle
- Construction: The controller is instantiated.
- Attachment: didAttachToScope is called when registered in a LevitScope.
- Initialization: onInit is called once after construction and attachment.
- Disposal: onClose is called when the controller's scope is disposed or it is removed.
Automated Cleanup
Use autoDispose to register objects for automatic cleanup during onClose. This prevents memory leaks by ensuring resources are disposed in a deterministic order.
// Example usage:
class MyController extends LevitController {
late final count = autoDispose(0.lx);
@override
void onInit() {
super.onInit();
// Start persistent listeners or fetch initial data
}
}
- Implemented types
- Implementers
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- initialized → bool
-
Whether onInit has been executed.
no setter
- isClosed → bool
-
Whether the controller is in the process of closing or is closed.
no setter
- isDisposed → bool
-
Whether the controller has been disposed and closed.
no setter
- isInitialized → bool
-
Whether the initialization phase is complete.
no setter
- ownerPath → String
-
The full owner path within the monitoring system (scopeId:registrationKey).
no setter
- registrationKey → String?
-
The registration key used to identify this instance in Levit.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- scope → LevitScope?
-
The LevitScope that currently owns this controller.
no setter
Methods
-
autoDispose<
T> (T object) → T -
Registers an
objectto be automatically cleaned up when this controller is closed. -
didAttachToScope(
LevitScope scope, {String? key}) → void -
Invoked when the instance is successfully attached to its owning LevitScope.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onClose(
) → void -
Callback invoked when the controller is being removed from Levit.
override
-
onInit(
) → void -
Callback invoked after the controller is instantiated and registered.
override
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited