Logosolidart

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)#

Returns the future that completes when the condition evalutes to true. If the condition is already true, it completes immediately.

For example:

final count = Signal(0);
await count.until((value) => value >= 10); // await until value >= 10

bool get disposed#

Returns true if the signal has been disposed.

void dispose()#

Disposes the signal. If the signal has already been disposed, this method does nothing. All observers will be unsubscribed.

String toString()#

Returns a string representation of the signal. For example:

final count = Signal(0);
count.value++;
print(count.toString());
// prints "ReadSignal<int>(value: 1, previousValue: 0, options: ...)"

toValueNotifier()#

Converts this ReadSignal into a ValueNotifier.

If you need to convert a ValueNotifier into a ReadSignal use toSignal() instead.