useExistingSignal#
function |
Package: package:signals_hooks
Function: useExistingSignal#
S useExistingSignal(S value, {List<Object?> keys = const <Object>[]})
Subscribes to an existing ReadonlySignal (or Signal) and returns it.
This is useful when you have a signal that is defined globally, inside a controller, or provided via a dependency injection framework (like provider, get_it, or riverpod), and you want to reactive-bind the HookWidget to rebuild whenever the signal emits a new value.
Parameters#
- value: The existing ReadonlySignal or Signal instance to subscribe to.
- keys: A list of objects to watch for changes. If any of the keys change, the hook is re-evaluated, unsubscribing from the old signal and subscribing to the new one.
Returns#
The same signal instance passed in value, allowing you to interact with it
directly (e.g., read its .value or write to it if it is a mutable Signal).
Example#
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:signals_hooks/signals_hooks.dart';
// A globally defined signal or one located in a service
final globalThemeMode = signal(ThemeMode.light);
class ThemeSelectorWidget extends HookWidget {
const ThemeSelectorWidget({super.key});
@override
Widget build(BuildContext context) {
// Bind to the existing global signal
final themeMode = useExistingSignal(globalThemeMode);
return SwitchListTile(
title: Text('Dark Mode: ${themeMode.value == ThemeMode.dark ? "On" : "Off"}'),
value: themeMode.value == ThemeMode.dark,
onChanged: (isDark) {
themeMode.value = isDark ? ThemeMode.dark : ThemeMode.light;
},
);
}
}
References#
The useExistingSignal type is referenced and used in the following pages:
- signals_hooks
- useSignal (signals_hooks/hooks)
- useSignalValue (signals_hooks/hooks)
- useExistingSignal (signals_hooks/hooks)