computed<T> function
FlutterComputed<T>
computed<T>(
- T compute(), {
- ComputedOptions<
T> ? options, - @Deprecated('Use options: ComputedOptions(name: ...) instead') String? debugLabel,
- @Deprecated('Use options: ComputedOptions(autoDispose: ...) instead') bool? autoDispose,
- bool runCallbackOnListen = false,
Creates a new FlutterComputed signal that recalculates its value dynamically
based on the signals accessed inside the compute callback.
The returned computed signal is read-only and cached. It will only re-evaluate when one of its tracked dependencies changes.
Flutter Example
final firstName = signal('John');
final lastName = signal('Doe');
// Automatically combines both signals and caches the result:
final fullName = computed(() => '${firstName.value} ${lastName.value}');
// Usage inside a widget build:
@override
Widget build(BuildContext context) {
return SignalBuilder(
builder: (context) => Text('Hello, ${fullName.value}!'),
);
}
Implementation
FlutterComputed<T> computed<T>(
T Function() compute, {
core.ComputedOptions<T>? options,
@Deprecated('Use options: ComputedOptions(name: ...) instead')
String? debugLabel,
@Deprecated('Use options: ComputedOptions(autoDispose: ...) instead')
bool? autoDispose,
bool runCallbackOnListen = false,
}) {
return FlutterComputed<T>(
compute,
options: options,
debugLabel: debugLabel,
autoDispose: autoDispose,
runCallbackOnListen: runCallbackOnListen,
);
}