skip to Main Content

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


  1. 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)

    Login or Signup to reply.
  2. 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 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search