update method

T update(
  1. T updater(
    1. T value
    )
)

Updates the value using an updater function based on the current value.

This method reads the current value using peek (without establishing a reactive dependency), passes it to the updater function, and then sets the new value using set. This is useful for updating values based on their current state without needing to manually read and write.

Implementation note: This method internally calls set with the result of applying the updater function to the current value obtained via peek.

Parameters:

  • updater: A function that takes the current value and returns a new value

Returns: The new value that was set

Example:

final count = Signal(5);
count.update((value) => value + 1); // count.value is now 6
count.update((value) => value * 2); // count.value is now 12

This is equivalent to:

count.set(count.peek + 1);
count.set(count.peek * 2);

Implementation

@pragma("vm:prefer-inline")
@pragma("wasm:prefer-inline")
@pragma("dart2js:prefer-inline")
T update(T Function(T value) updater) => set(updater(peek));