Signal<T> class

Signals

Signals are the cornerstone of reactivity in solidart. They contain values that change over time; when you change a signal's value, it automatically updates anything that uses it.

Create the signal with:

final counter = Signal(0);

The argument passed to the create call is the initial value, and the return value is the signal.

To retrieve the current signal value use:

counter.value; // 0
// or
counter(); // 0

To update the current signal value you can use:

counter.value++; // increase by 1
// or
counter.set(2); // sets the value to 2
// or
counter.value = 5; // sets the value to 5
// or
counter.update((v) => v * 2); // update based on the current value

Derived Signals

You may want to subscribe only to a sub-field of a Signal value.

// sample User class
class User {
  const User({
    required this.name,
    required this.age,
  });

  final String name;
  final int age;

  User copyWith({
    String? name,
    int? age,
  }) {
    return User(
      name: name ?? this.name,
      age: age ?? this.age,
    );
  }
}

// create a user signal
final user = Signal(const User(name: "name", age: 20));

// create a derived signal just for the age
final age = Computed(() => user().age);

// adding an effect to print the age
Effect((_) {
  print('age changed from ${age.previousValue} into ${age.value}');
});

// just update the name, the effect above doesn't run because the age has not changed
user.update((value) => value.copyWith(name: 'new-name'));

// just update the age, the effect above prints
user.update((value) => value.copyWith(age: 21));

A derived signal is not of type Signal but is a ReadSignal. The difference with a normal Signal is that a ReadSignal doesn't have a value setter, in other words it's a read-only signal.

You can also use derived signals in other ways, like here:

final counter = Signal(0);
final doubleCounter = Computed(() => counter() * 2);

Every time the counter signal changes, the doubleCounter updates with the new doubled counter value.

You can also transform the value type into a bool:

final counter = Signal(0); // type: int
final isGreaterThan5 = Computed(() => counter() > 5); // type: bool

isGreaterThan5 will update only when the counter value becomes lower/greater than 5.

  • If the counter value is 0, isGreaterThan5 is equal to false.
  • If you update the value to 1, isGreaterThan5 doesn't emit a new value, but still contains false.
  • If you update the value to 6, isGreaterThan5 emits a new true value.
Inheritance
Implementers
Available extensions

Constructors

Signal(T initialValue, {String? name, bool? equals, bool? autoDispose, bool? trackInDevTools, ValueComparator<T?> comparator = identical, bool? trackPreviousValue})
Signals
Signal.lazy({String? name, bool? equals, bool? autoDispose, bool? trackInDevTools, ValueComparator<T?> comparator = identical, bool? trackPreviousValue})
Signals

Properties

autoDispose bool
Whether to automatically dispose the signal (defaults to SolidartConfig.autoDispose).
finalinherited
comparator → ValueComparator<T?>
An optional comparator function, defaults to identical.
finalinherited
disposed bool
Tells if the signal is disposed;
no setterinherited
equals bool
Whether to check the equality of the value with the == equality.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
hasPreviousValue bool
Indicates if there is a previous value. It is especially helpful if T is nullable.
no setterinherited
hasValue bool
Whether or not the signal has been initialized with a value.
no setterinherited
listenerCount int
Returns the number of listeners listening to this signal.
no setterinherited
name String?
The name of the signal, useful for logging purposes.
finalinherited
previousValue → T?
The previous value, if any.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
trackInDevTools bool
finalinherited
trackPreviousValue bool
finalinherited
untrackedPreviousValue → T?
Returns the untracked previous value of the signal.
no setterinherited
untrackedValue → T
Returns the value without triggering the reactive system.
no setterinherited
value ↔ T
The current signal value
getter/setter pairinherited-getter

Methods

call() → T
The current signal value
inherited
dispose() → void
Diposes the signal
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
observe(ObserveCallback<T> listener, {bool fireImmediately = false}) DisposeObservation

Available on SignalBase<T>, provided by the ObserveSignal extension

Observe the signal and trigger the listener every time the value changes
onDispose(VoidCallback cb) → void
Fired when the signal is disposing
inherited
setValue(T newValue) → T
Sets the current signal value with newValue.
inherited
shouldUpdate() bool
Indicates if the signal should update its value.
inherited
toggle() → void

Available on Signal<bool>, provided by the ToggleBoolSignal extension

Toggles the signal boolean value.
toReadSignal() ReadableSignal<T>
Converts this Signal into a ReadableSignal Use this method to remove the visility to the value setter.
toString() String
A string representation of this object.
override
until(bool condition(T value), {Duration? timeout}) FutureOr<T>

Available on SignalBase<T>, provided by the Until extension

Returns the future that completes when the condition evaluates to true. If the condition is already true, it completes immediately.
updateValue(T callback(T value)) → T
Calls a function with the current value and assigns the result as the new value.

Operators

operator ==(Object other) bool
The equality operator.
inherited