The ReactterConsumer widget obtains the dependency provided by the closest ReactterProvider widget and rebuilds itself whenever there are changes in the dependency or any defined states.
Syntax
Properties
key: An optional Key to use for identifying the widget.
id: An optional identifier for the dependency.
If omitted, the dependency will be located by its type(T).
child: An optional Widget that is independent of the ReactterConsumer.
If defined, it is passed to the builder function if it is defined.
listenAll: A boolean that determines whether to listen to all states provided by the T dependency.
If set to true, the listenStates function is ignored.
The default value is false.
listenStates: A function that returns a list of state(ReactterState) to listen to.
It takes the instance of the T dependency as an åargument.
If omitted, the listenAll property is checked.
builder: A function that builds a widget depending on the ReactterConsumer.
It receives the following arguments:
context: The BuildContext of the widget. A handle to the location of ReactterConsumer in the widget tree.
instance: The instance of T dependency provided by the closest ReactterProvider widget.
child: The child widget passed to the ReactterConsumer widget.
Usage
Basic Usage
To use the ReactterConsumer widget, you need to provide the T dependency using the ReactterProvider widget.
Here’s an example of how to use it:
In the example above, the ReactterConsumer widget is used only to access the CounterController dependency within the Counter widget.
Consequently, it does not rebuild whenever there are changes in the count state of the CounterController dependency.
Listening to all state
The ReactterConsumer widget can listen to all states provided by the T dependency by setting the listenAll property to true.
Here’s an example of how to use it:
In the example above, the ReactterConsumer widget will trigger a rebuild of the widget subtree(the Row widget and each of its children) whenever there are changes in any state of the CounterController dependency.
So, that approach lacks performance efficient.
It is advisable to optimize by selectively wrapping only the necessary widgets with the ReactterConsumer widget, utilizing either active or non-active listening as appropriate, e.g.:
Listening to specific states
The ReactterConsumer widget can listen to specific states provided by the T dependency by defining the listenStates property.
Here’s an example of how to use it:
In the example above, the ReactterConsumer widget will trigger a rebuild of the Text widget whenever there are changes in the count state of the CounterController dependency.
Using id
The ReactterConsumer widget can use the id property to locate the T dependency by its identifier.
It is useful when there are multiple instances of the same type of dependency in the widget tree.
Here’s an example of how to use it:
In the example above, the ReactterConsumer widget uses the id property to locate the CounterController dependency by the counter1 and counter2 identifiers.
Using child
The ReactterConsumer widget can accept a child property that is independent of the ReactterConsumer widget.
It is useful when you want to provide a widget that does not rebuild when the dependency changes.
Here’s an example of how to use it:
In the example above, the ReactterConsumer widget uses the child property to pass the Text widget to the builder function.