select<T extends Object?, R> method

R select<T extends Object?, R>(
  1. Selector<T, R> selector, [
  2. String? id
])

Uses the selector callback(first argument), for determining if the widget tree of context needs to be rebuild by comparing the previous and new result of selector, and returns it. This evaluation only occurs if one of the selected ReactterStates gets updated, or by the instance if the selector does not have any selected ReactterStates.

The selector callback has a two arguments, the first one is the instance of T type which is obtained from the closest ancestor of ReactterProvider and the second one is a Select function which allows to wrapper any ReactterStates to listen.

final int value = context.select<MyController, int>(
  (inst, $) => $(inst.stateA).value.length,
);
// more that one states can be wrapped with Select($):
final String value = context.select<MyController, String>(
  (inst, $) {
    final stateA = $(inst.stateA).value.trim();
    final stateB = $(inst.stateB).value.trim();

    return '$stateA $stateB';
  },
);
// or simply donĀ“t use Select($) and react with to any changes in the instance(low performance)
final int value = context.select<MyController, int>(
  (inst, $) => inst.stateA.value.length,
);

If T is not defined and ReactterScope is not found, will throw ReactterScopeNotFoundException.

If T is non-nullable and the instance obtained returned null, will throw ReactterInstanceNotFoundException.

If T is nullable and no matching instance is found, Selector first argument will return null.

This method is equivalent to calling:

ReactterSelector.contextOf<T>(context, selector, id);

Implementation

R select<T extends Object?, R>(
  Selector<T, R> selector, [
  String? id,
]) {
  return ReactterSelector.contextOf(this, id: id, selector: selector);
}