ReactterSelector<T extends Object?, V> class

A StatelessWidget similar to ReactterConsumer but allowing to control the rebuilding of widget tree by selecting the ReactterStates, and a computed value.

ReactterSelector determines if builder needs to be rebuild again 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 property has a two arguments, the first one is the instance of T type which is obtained from the closest ancestor ReactterProvider. and the second one is a Select function which allows to wrapper any ReactterStates to listen, and returns the value in each build. e.g:

ReactterSelector<MyController, double>(
  selector: (MyController inst, Select $) {
    final num1 = $(inst.intState).value;
    final num2 = $(inst.doubleState).value;

    return (num1 + num2).clamp(0, 100);
  },
  builder: (BuildContext context, MyController inst, double value, Widget? child) {
    return Text('Computed value: $value');
  },
)

It does not have the T tyoe, it is necessary to wrap the app with ReactterScope for it to work properly. e.g:

ReactterScope(
  child: MyApp(),
)

// Now can use it without type like:

ReactterSelector(
  selector: (_, $) => $(myListState).value.length,
  builder: (context, _, itemCount, child) {
    return ListView.builder(
      itemCount: itemCount,
      itemBuilder: (context, index) {
        [...]
      },
    );
  },
)

ReactterSelector has same functionality as ReactterSelector.contextOf.

Use id property to identify the T instance.

Use child property to pass a Widget which to be built once only. It will be sent through the builder callback, so you can incorporate it into your build:

ReactterSelector<MyController, String>(
  selector: (inst, $) => $(inst.strState).value.trim(),
  child: Text("This widget build only once"),
  builder: (context inst, value, child) {
    return Column(
      children: [
        Text("value: $value"),
        child,
      ],
    );
  },
)

See also:

Inheritance

Constructors

ReactterSelector({Key? key, String? id, required Selector<T, V> selector, Widget? child, required InstanceValueChildBuilder<T, V> builder})
A StatelessWidget similar to ReactterConsumer but allowing to control the rebuilding of widget tree by selecting the ReactterStates, and a computed value.
const

Properties

builder InstanceValueChildBuilder<T, V>
Method which is responsible for building the widget tree.
final
child Widget?
Use to pass a single child widget that will be built once and incorporated into the widget tree.
final
hashCode int
The hash code for this object.
no setterinherited
id String?
This identifier can be used to differentiate between multiple instances of the same T type in the widget tree when using ReactterProvider.
final
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
selector Selector<T, V>
Takes an instance of T type and a Select function, and returns a value of R type. It can be used to compute a value based on the provided arguments.
final

Methods

build(BuildContext context) Widget
Describes the part of the user interface represented by this widget.
override
createElement() StatelessElement
Creates a StatelessElement to manage this widget's location in the tree.
inherited
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

contextOf<T extends Object?, V>(BuildContext context, {String? id, required Selector<T, V> selector}) → V
Uses the selector callback, for determining if the widget tree of context needs to be rebuild by comparaing 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.