NeuronController class abstract
============================================================================ 2. BASE CONTROLLER
Base class for all Neuron controllers.
Controllers in Neuron serve as the business logic layer, managing state (signals) and operations. They extend NeuronController to get automatic disposal and lifecycle hooks.
Lifecycle
Controllers have two lifecycle hooks:
- onInit: Called once after the controller is registered via Neuron.install or Neuron.ensure
- onClose: Called before the controller is disposed via Neuron.uninstall
Auto-disposal
Any signal that calls SignalBinding.bind with the controller will be automatically disposed when the controller is disposed.
Signal Creation Syntax
Three equivalent ways to create bound signals:
class MyController extends NeuronController {
// 1. Verbose: explicit constructor + bind
late final count1 = Signal<int>(0).bind(this);
// 2. Clean: factory methods (recommended)
late final count2 = signal(0);
late final doubled = computed(() => count2.val * 2);
late final user = asyncSignal<User>();
// 3. Ultra-short: $ prefix
late final count3 = $(0);
late final tripled = $computed(() => count3.val * 3);
late final posts = $async<List<Post>>();
}
Lifecycle Hooks
class MyController extends NeuronController {
late final count = signal(0);
@override
void onInit() {
print('Controller initialized');
}
@override
void onClose() {
print('Controller disposed');
}
}
Static Init Pattern
The recommended pattern is to provide a static getter:
static MyController get init => Neuron.ensure<MyController>(() => MyController());
This ensures the controller is created once and reused across the app.
Lifecycle
- onInit: Called when the controller is first created.
- onClose: Called when the controller is disposed.
Side Effects
Use effect to run code in response to signal changes.
@override
void onInit() {
effect(() {
print('Count changed: ${count.value}');
}, [count]);
}
See also:
- Neuron - Service locator for managing controllers
- SignalBinding - Extension for auto-disposal
- Implementers
- Available extensions
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
$<
T> (T initial, {String? debugLabel, bool equals(T prev, T next)?, T guard(T current, T next)?, VoidCallback? onListen, VoidCallback? onCancel}) → Signal< T> -
Available on NeuronController, provided by the NeuronControllerShorthand extension
Creates a Signal with ultra-short syntax. -
$async<
T> ({T? initial, String? debugLabel}) → AsyncSignal< T> -
Available on NeuronController, provided by the NeuronControllerShorthand extension
Creates an AsyncSignal with ultra-short syntax. -
$computed<
T> (T computation(), {String? debugLabel}) → Computed< T> -
Available on NeuronController, provided by the NeuronControllerShorthand extension
Creates a Computed signal with ultra-short syntax. -
asyncSignal<
T> ({T? initial, String? debugLabel}) → AsyncSignal< T> -
Available on NeuronController, provided by the NeuronControllerSignals extension
Creates an AsyncSignal and automatically binds it to this controller. -
computed<
T> (T computation(), {String? debugLabel}) → Computed< T> -
Available on NeuronController, provided by the NeuronControllerSignals extension
Creates a Computed signal and automatically binds it to this controller. -
dispose(
) → void - Disposes all registered notifiers and calls onClose.
-
effect(
void callback(), List< NeuronAtom> dependencies, {bool fireImmediately = true}) → void - Run a side effect whenever dependencies change.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onClose(
) → void - Called right before the controller is disposed.
-
onInit(
) → void - Called once after the controller is installed via Neuron.install or Neuron.ensure.
-
signal<
T> (T initial, {String? debugLabel, bool equals(T prev, T next)?, T guard(T current, T next)?, VoidCallback? onListen, VoidCallback? onCancel}) → Signal< T> -
Available on NeuronController, provided by the NeuronControllerSignals extension
Creates a Signal and automatically binds it to this controller. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited