SetupHook<T> class
abstract
Base class for all setup hooks.
Hooks provide a way to encapsulate reusable stateful logic in SetupWidget. Each hook has a lifecycle that mirrors the widget lifecycle.
Lifecycle Methods
- build: Called once to create the initial state (on first use)
- mount: Called after the hook is created and added to the widget
- unmount: Called when the hook is being removed (on dispose or hot reload mismatch)
- didUpdateWidget: Called when the widget is updated with new properties
- didChangeDependencies: Called when the widget's dependencies change
- activated: Called when the widget is reactivated (e.g., after being in a navigator stack)
- deactivated: Called when the widget is deactivated
- reassemble: Called during hot reload when the hook is reused
Hot Reload Behavior
During hot reload, hooks are matched by their runtime type and position:
- If the hook type at position N matches, the hook is reused and reassemble is called
- If there's a type mismatch, this and all subsequent hooks are unmounted
- New or replacement hooks have mount called
Example:
class TimerHook extends SetupHook<Timer> {
@override
Timer build() => Timer.periodic(Duration(seconds: 1), (_) {});
@override
void mount() {
print('Timer started');
}
@override
void unmount() {
state.cancel();
print('Timer cancelled');
}
@override
void reassemble() {
print('Hot reload - timer preserved');
}
}
- Implementers
Constructors
Properties
- context → BuildContext
-
The BuildContext of the widget that owns this hook.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → T
-
The current state value of this hook.
no setter
Methods
-
activated(
) → void - Called when the widget is reactivated.
-
build(
) → T - Builds the initial state for this hook.
-
deactivated(
) → void - Called when the widget is deactivated.
-
didChangeDependencies(
) → void - Called when the widget's InheritedWidget dependencies change.
-
didUpdateWidget(
) → void - Called when the parent widget is updated with new properties.
-
mount(
) → void - Called after the hook is created and added to the widget.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reassemble(
) → void - Called during hot reload when this hook is reused.
-
toString(
) → String -
A string representation of this object.
inherited
-
unmount(
) → void - Called when the hook is being removed.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited