writable<T> method

WritableComputed<T> writable<T>(
  1. T getter(),
  2. void setter(
    1. T
    ), {
  3. List<Object?>? keys,
  4. JoltDebugOption? debug,
})

Creates a writable computed signal hook with custom getter and setter.

A writable computed signal allows you to define custom read and write behavior, enabling two-way data binding with derived values.

Parameters:

  • getter: Function that computes the current value
  • setter: Function that handles value updates
  • keys: Optional keys for hook memoization
  • debug: Optional debug options

Returns: A WritableComputed with custom read/write behavior

Example:

Widget build(BuildContext context) {
  final count = useSignal(0);
  final doubled = useComputed.writable(
    () => count.value * 2,
    (value) => count.value = value ~/ 2,
  );

  return Column(
    children: [
      Text('Count: ${count.value}'),
      Text('Doubled: ${doubled.value}'),
      ElevatedButton(
        onPressed: () => doubled.value = 10,
        child: Text('Set doubled to 10'),
      ),
    ],
  );
}

Implementation

WritableComputed<T> writable<T>(
  T Function() getter,
  void Function(T) setter, {
  List<Object?>? keys,
  JoltDebugOption? debug,
}) {
  return use(
    JoltHook(() => WritableComputed(getter, setter, debug: debug),
        keys: keys),
  );
}