reactionWithLocator<T> method

ReactionDisposer reactionWithLocator<T>(
  1. LocatorReactionFn<T> fn,
  2. ReactionEffect<T> effect, {
  3. String? name,
  4. int? delay,
  5. bool? fireImmediately,
  6. EqualityComparer<T>? equals,
  7. ReactiveContext? context,
  8. OnError? onError,
})

Executes the fn function and tracks the observables used in it, re-executing whenever the dependent observables change or the observable locator scope changes. If the T value returned by fn is different, the effect function is executed.

The reaction will be disposed automatically when the widget state is disposed.

Returns a disposer that can be used to dispose the reaction early.

Note: Only the fn function is tracked and not the effect.

This function can safely be called in initState. If called in a method that is run multiple times such as didUpdateWidget, it is the responsibility of the caller to dispose of older stale reactions.

Implementation

ReactionDisposer reactionWithLocator<T>(
  LocatorReactionFn<T> fn,
  ReactionEffect<T> effect, {
  String? name,
  int? delay,
  bool? fireImmediately,
  EqualityComparer<T>? equals,
  ReactiveContext? context,
  OnError? onError,
}) {
  final reaction = _ReactionFnDef(
      fn, effect, name, delay, fireImmediately, equals, context, onError,
      (reaction) {
    _reactions.remove(reaction);
  });
  _addReaction(reaction);
  return reaction.dispose;
}