writable<T> method

Derived<T> writable<T>(
  1. T getter(
    1. T? oldValue
    ),
  2. void setter(
    1. T newValue
    )
)

Creates a writable derived reference.

The getter function computes the value based on the old value. The setter function is used to update the value call.

final count = ref(0);
final doubleCount = derived.writable<int>(
  (_) => count.value * 2, // compute double value
  (value) => count.value = value ~/ 2, // update count value
);

doubleCount.value = 10; // count.value will be 5
print(count.value); // 5

count.value = 10; // doubleCount.value will be 20
print(doubleCount.value); // 20

Implementation

public.Derived<T> writable<T>(
  T Function(T? oldValue) getter,
  void Function(T newValue) setter,
) {
  return impl.Derived(getter, setter);
}