Effect
Signals are trackable values, but they are only one half of the equation. To complement those are observers that can be updated by those trackable values. An effect is one such observer; it runs a side effect that depends on signals.
An effect can be created by using the Effect
class.
The effect automatically subscribes to any signal and reruns when any of them change.
So let’s create an Effect that reruns whenever counter
changes:
final disposeEffect = Effect(() { print("The count is now ${counter.value}");});
The effect run immediately and prints The count is now 0
;
Try incrementing the counter by 1:
counter.value++;
The effect prints The count is now 1
;
The Effect
class returns a Dispose
callback, invoke it to stop listening and clearing the effect.
final disposeEffect = Effect(() { print("The count is now ${counter.value}");});
// Somewhere else, dispose the effectdisposeEffect();