Skip to content

ValueListenableSignalMixin

ValueListenableSignalMixin is a mixin for a Readonly Signal that implements ValueListenable.

This allows you to use the signal as a ValueListenable in Flutter widgets.

class MySignal extends Signal<int> with ValueListenableSignalMixin<int> {
MySignal(int value) : super(value);
}
void main() {
final signal = MySignal(0);
assert(signal is ReadonlySignal<int>);
assert(signal is ValueListenable<int>);
final listener = () => print(signal.value);
signal.addListener(listener);
signal.value = 1;
signal.removeListener(listener);
signal.value = 2;
}

When addListener is called it will subscribe to the signal and call the listener when the signal changes.

When removeListener is called with the same method it will unsubscribe from the signal.

When the signal is disposed it will remove all listeners.

ValueListenableBuilder

In Flutter you can use the ValueListenableBuilder widget to listen to a ValueListenable.

import 'package:flutter/material.dart';
import 'package:signals/signals_flutter.dart';
final counter = signal(0);
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ValueListenableBuilder<int>(
valueListenable: counter,
builder: (context, value, child) {
return Text('Count: $value');
},
);
}
}