The RtConsumer widget obtains the dependency provided by the closest RtProvider 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 remains static while the widget tree is rebuilt.
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: An optional function that returns a list of state(RtState) 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 rebuilds a widget depending on the RtConsumer.
It receives the following arguments:
context: The BuildContext of the RtConsumer widget.
instance: The instance of T dependency provided by the closest RtProvider widget.
child: The child widget passed to RtConsumer.
Usage
Basic Usage
To use RtConsumer, you need to provide the T dependency using the RtProvider widget.
Here’s an example of how to use it:
counter.dart
main.dart
counter_view.dart
counter_controller.dart
In the example above, RtConsumer 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
To listen to all states, set the listenAll property to true. This ensures that RtConsumer rebuilds whenever any state in the dependency changes.
Here’s an example of how to use it:
counter.dart
main.dart
counter_view.dart
counter_controller.dart
In the example above, RtConsumer 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 RtConsumer, utilizing either active or non-active listening as appropriate, e.g.:
counter.dart
main.dart
counter_view.dart
counter_controller.dart
Listening to specific states
To listen to specific states, use the listenStates property.
Define a function that returns a list of the states you want to listen to.
This ensures that RtConsumer rebuilds only when the specified states change.
Here’s an example of how to use it:
counter.dart
main.dart
counter_view.dart
counter_controller.dart
In the example above, RtConsumer will trigger a rebuild of the Text widget whenever there are changes in the count state of the CounterController dependency.
Using id
RtConsumer offers the id property to locate the T dependency by its identifier.
This is useful when you have multiple instances of the same dependency type and need to specify which one to use.
Here’s an example of how to use it:
counter.dart
counter_view.dart
counter_view.dart
counter_controller.dart
In the example above, RtConsumer uses the id property to locate the CounterController dependency by the counter1 and counter2 identifiers.
Using child
RtConsumer offers the child property to pass a widget that is independent of the RtConsumer and is passed to the builder function.
This is useful when you want to reuse a widget that does not need to rebuild when the dependency changes.
Here’s an example of how to use it:
counter.dart
counter_view.dart
main.dart
counter_controller.dart
In the example above, RtConsumer uses the child property to pass the Text widget to the builder function.