ReadSignal API docs
A read-only Signal.
All Computed signals are __ReadSignal__s.
If you want to convert a Signal into a ReadSignal use the toReadSignal method
Constructor
ReadSignal(
T initialValue, {
SignalOptions<T>? options,
});
initialValue
is the initial value of the signal.
options
are the options of the signal.
T get value
Returns the current signal value.
For example:
final count = Signal(0);
print(count.value); // prints 0
T call()
Returns the current signal value.
For example:
final count = Signal(0);
print(count()); // prints 0
bool get hasPreviousValue
Returns true
if the signal has a previous value.
For example:
final count = Signal(0);
print(count.hasPreviousValue); // prints false
count.value++;
print(count.hasPreviousValue); // prints true
T? get previousValue
Returns the previous value, or null
if the signal has no previous value.
For example:
final count = Signal(0);
print(count.previousValue); // prints null
count.value++;
print(count.previousValue); // prints 0
DisposeObservation observe(ObserveCallback listener, {bool fireImmediately = false})
Observe the signal and trigger the listener every time the value changes with the previous and current values.
For example:
final count = Signal(0);
count.observe((previousValue, value) {
print("The count changed from $previousValue to $value");
});
count.value++;
// prints "The count changed from 0 to 1"
The observation will not be fired when created with the current value, if you want to run it at creation, set fireImmediately
to true
, for example:
final count = Signal(0);
count.observe((previousValue, value) {
print("The count changed from $previousValue to $value");
}, fireImmediately: true);
// prints "The count changed from null to 0"
FutureOr<T> until(bool Function(T value) condition, {Duration? timeout})
Returns the future that completes when the condition evalutes to true. If the condition is already true, it completes immediately.
The optional timeout parameter specifies the maximum time to wait for the condition to be met. If provided and the timeout is reached before the condition is met, the future will complete with a TimeoutException.
For example:
final count = Signal(0);
await count.until((value) => value >= 10); // await until value >= 10
// With timeout - will throw TimeoutException if condition not met in 5 seconds
await count.until(
(value) => value >= 10,
timeout: Duration(seconds: 5),
);
void dispose()
Disposes the signal. If the signal has already been disposed, this method does nothing. All observers will be unsubscribed.