Эта страница ещё не переведена, поэтому показана на английском. Помогите с переводом

fxOnChange

Runs a callback only when its argument differs from the last one — uniqAdjacent, for a function.

OnChange<T> fxOnChange<T>(void Function(T arg) func, {bool Function(T previous, T next)? equals}) class OnChange<T> { void call(T arg); // OnChange is callable: onChange(arg) void reset(); // forget the last value } OnChange<T> (void Function(T arg)).fxOnChange({bool Function(T previous, T next)? equals}) // method

Lecture

A value polled on a timer or recomputed every frame — an elapsed second read four times a second, a coin count, a progress fraction — arrives far more often than it changes. The guard is always the same three lines: remember the last value, compare, return early. fxOnChange(f) is those lines once. The first call always runs; after that a call runs f only when its argument is not equal to the argument of the last call that did.

Equality is == by default; pass equals to decide what "changed" means — a rounding bucket, a field of a record, a case-insensitive string. .reset() forgets the last value, so the next call runs whatever it carries. Like fxDebounce it has a method spelling on the callback: setCoins.fxOnChange().

It is the callback twin of uniqAdjacent: the same rule, for a value pushed into a function rather than pulled from a sequence. Unlike a debounce it adds no delay and drops nothing that changed. fxdart extension (not part of FxTS).

Demo 1 · A polled value

Demo 2 · equals and reset()

Try it yourself

Exercise: stop writing a coin count that did not change.

Related: uniqAdjacent — the same rule on a sequence · fxDebounce — wait for quiet · fxThrottle — at most once per window · fxMemoizeLast — skip recomputing for the same input