toSignal method
Bi-directional Signal and ValueNotifier Interoperability
Converted ValueNotifier objects become mutable Signal instances. Setting the value on the signal or the notifier automatically propagates the update to the other.
The subscription is fully memory-safe and automatically unsubscribed when the signal is disposed.
Example: Converting a ValueNotifier to a Signal
final notifier = ValueNotifier(10);
final signal = notifier.toSignal();
signal.value = 20;
print(notifier.value); // 20
notifier.value = 30;
print(signal.value); // 30
Example: Converting a Signal to a ValueNotifier
To bridge back to a standard ValueNotifier for Flutter compatibility:
final signal = Signal(10);
final notifier = signal.toValueNotifier();
Implementation
Signal<T> toSignal({
String? debugLabel,
bool autoDispose = false,
}) {
bool? self;
final target = signal<T>(
value,
debugLabel: debugLabel,
autoDispose: autoDispose,
);
void updater() {
if (self == false) {
// Update from signal
} else {
// Normal flow
self = true;
target.value = value;
self = null;
}
}
addListener(updater);
target.onDispose(() => removeListener(updater));
target.subscribe((val) {
if (self == true) {
// Update from value notifier
} else {
// Normal flow
self = false;
value = val;
self = null;
}
});
return target;
}