BloomController class abstract
Base class for stateful controllers and view-models in Bloom JS Native applications.
BloomController encapsulates business logic, reactive signals, and lifecycle management without any dependency on the Flutter SDK. It provides automatic cleanup tracking for signal effect callbacks via addEffect and arbitrary teardown logic via autoDispose.
Lifecycle & Dependency Injection
- Initialization: Subclasses override onInit to set up event listeners, initial fetches, or reactive subscriptions. onInit is called synchronously in the constructor.
- Disposal: When the controlling component or container is torn down, call onDispose. This cancels all effect subscriptions registered with addEffect and executes all cleanup callbacks registered with autoDispose. Disposal is idempotent and swallows exceptions from individual callbacks to guarantee all disposers run.
- DI Integration: Controllers are typically registered in the BloomContainer as
transients (per-view) or singletons (application-wide):
provideSingleton(() => AuthController()); provide(() => UserProfileController(inject()));
Backend Behavior
- Browser (
mount): Subscribed effects and reactive bindings execute on signal mutations. Controllers should be explicitly disposed when components unmount or routers transition. - SSR (
renderToHtml): Safe to instantiate during SSR to compute initial state descriptors. Avoid creating persistent timers or unclosed stream subscriptions during SSR.
Example
class CounterController extends BloomController {
final count = signal(0);
late final ReadonlySignal<bool> isEven;
@override
void onInit() {
super.onInit();
isEven = computed(() => count.value.isEven);
// Automatically cleaned up on onDispose()
addEffect(() {
// Triggers whenever count.value changes
});
}
void increment() => count.value++;
void decrement() => count.value--;
}
See also:
- BloomContainer, the dependency injection container used to register and resolve controllers.
Constructors
- BloomController()
- Creates a BloomController and immediately invokes onInit.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isDisposed → bool
-
Whether this controller has been disposed via onDispose.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addEffect(
void effectCb(), {String? debugLabel}) → void - Registers a reactive effect that is automatically disposed when this controller is disposed.
-
autoDispose(
void cleanup()) → void - Registers a custom cleanup callback to execute when onDispose is called.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onDispose(
) → void - Disposes all registered effects, cleanup callbacks, and resources.
-
onInit(
) → void - Called immediately during controller instantiation.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited