I looked at Riverpod’s counter example and noticed it uses ConsumerWidget instead of ConsumerStatefulWidget.
If ConsumerWidget can show state changes, is there any need to use ConsumerStatefulWidget? Why Riverpod, as a state management solution, has both stateful and stateless consumer widgets? It seems there is something I haven’t yet comprehend
2
Answers
ConsumerStatefulWidget is here for if you want local state in your widget. Like instantiating an AnimationController
Typically providers are for shared state. But they don’t deal with local state.
Hence why you still sometimes need Statefulwidgets (or flutter_hooks if that’s your thing)
I’ll also add that we can quickly enough replace
StatefulWidget
withConsumerStatefulWidget
(andState
withConsumerState
) to access theref
variable throughout the widget. This is very useful when we have a previously written complexStatefulWidget
widget with lots of controllers (well, just imagine!) and suddenly we need to accessref
and useRiverpod
to its full potential.Then it becomes obvious and looks amazing!
Afterword: later on you will also find out that
ConsumerWidget
is actually aConsumerStatefulWidget
🙂