createComputed<T> function

  1. @Deprecated('Use Computed instead')
Computed<T> createComputed<T>(
  1. T selector(), {
  2. SignalOptions<T>? options,
})

A special Signal that notifies only whenever the selected values change.

You may want to subscribe only to sub-field of a Signal value or to combine multiple signal values.

// first name signal
final firstName = Signal('Josh');

// last name signal
final lastName = Signal('Brown');

// derived signal, updates automatically when firstName or lastName change
final fullName = Computed(() => '${firstName()} ${lastName()}');

print(fullName()); // prints Josh Brown

// 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 like:

final counter = Signal(0); // counter contains the value type `int`
final isGreaterThan5 = Computed(() => counter() > 5); // isGreaterThan5 contains the value 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.

Implementation

@Deprecated('Use Computed instead')
Computed<T> createComputed<T>(
  T Function() selector, {
  SignalOptions<T>? options,
}) =>
    Computed<T>(selector, options: options);