Home > Software design >  Why Riverpod's ConsumerWidget can show state changes?
Why Riverpod's ConsumerWidget can show state changes?

Time:12-07

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

CodePudding user response:

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)

CodePudding user response:

I'll also add that we can quickly enough replace StatefulWidget with ConsumerStatefulWidget (and State with ConsumerState) to access the ref variable throughout the widget. This is very useful when we have a previously written complex StatefulWidget widget with lots of controllers (well, just imagine!) and suddenly we need to access ref and use Riverpod to its full potential.

Then it becomes obvious and looks amazing!

Afterword: later on you will also find out that ConsumerWidget is actually a ConsumerStatefulWidget :)

  • Related