useSignal<T> function
FlutterSignal<T>
useSignal<T>(
- T value, {
- List<
Object?> keys = const <Object>[], - SignalOptions<
T> ? options,
Creates a new Signal that persists across widget rebuilds and subscribes to it.
The signal is instantiated once using useMemoized and automatically disposed of or cleaned up
if necessary (signals created in this manner are managed by the Hook lifetime). The widget
will automatically rebuild whenever this signal's value changes.
Parameters
value: The initial value of the signal.keys: A list of objects to watch for changes. If any key in this list changes, the signal is re-created with the currentvalueas its initial value.debugLabel: An optional debug label to identify the signal in developer tools.
Returns
A local FlutterSignal instance representing the reactive state.
Example
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:signals_hooks/signals_hooks.dart';
class CounterHookWidget extends HookWidget {
const CounterHookWidget({super.key});
@override
Widget build(BuildContext context) {
// Create a local signal managed by this HookWidget's lifecycle
final count = useSignal(0, keys: const [], options: SignalOptions(name: 'localCounter'));
return Scaffold(
body: Center(
child: Text(
'Count: ${count.value}',
style: Theme.of(context).textTheme.headlineMedium,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => count.value++,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
Implementation
FlutterSignal<T> useSignal<T>(
T value, {
List<Object?> keys = const <Object>[],
SignalOptions<T>? options,
}) {
final s = useMemoized(() => signal(value, options: options), keys);
return useExistingSignal(s, keys: keys);
}