setup abstract method
The setup function that runs once when the widget is created.
This function is called during the first build (after initState but before the first frame is rendered). It executes only once during the widget's lifetime.
Use this function to:
- Initialize reactive state with hooks (useSignal, useComputed, etc.)
- Set up side effects with useEffect
- Access widget properties via the props getter
- Create the widget builder function
Parameters
context: The BuildContext for this State. Note that you can access widget properties via the props getter instead ofcontext.widget
Returns
A WidgetFunction that will be called on each rebuild triggered by reactive dependencies. The builder should be a pure function that returns a Widget based on the current reactive state.
Example
@override
setup(context) {
final count = useSignal(0);
final doubled = useComputed(() => count.value * 2);
useEffect(() {
print('Count changed: ${count.value}');
}, [count.value]);
// Return the builder function
return () => Text('Count: ${count.value}, Doubled: ${doubled.value}');
}
Accessing InheritedWidgets
Unlike initState, the setup function can safely access InheritedWidgets:
@override
setup(context) {
final theme = Theme.of(context);
final count = useSignal(0);
return () => Text(
'Count: ${count.value}',
style: theme.textTheme.bodyLarge,
);
}
Implementation
WidgetFunction<T> setup(BuildContext context);