So, I am fairly new to Riverpod and I use Riverpod mainly because of less page rebuild but when should I use ConsumerWidget as I saw when I use ConsumerWidget it rebuilds the whole widget tree but when using only consumer on that button that it only rebuilds the button’s state not whole page.
so, when should I use ConsumerWidget and Consumer?
2
Answers
Usually
ConsumerWidget
is used for pages/UI components.For example:
On the other hand
Consumer
is used to watch the provider values locally.For example:
It’s recommended to use
Consumer
when you need to update a little chunk of your widget tree where re-rendering the whole tree is costly.It’s a matter of choice. You could use Consumer everywhere, or ConsumerWidget instead.
The recommendation is to generally use ConsumerWidget, for the fact that:
For example you could do:
In that scenario, if
SomeOtherPage.label
changes,MyTest
will correctly not rebuild and instead only rebuild ifmyProvider
changes.