0

Recently I discovered that we can access the provider value using either

Consumer<Model>(build: (context, value, child){
    return Widget(child: value.valueName),
  },
 ) 

Or using

context.watch<Model>().valueName

What is the best practice?

kartik
  • 93
  • 9
  • Does this answer your question? [When to use Provider.of vs. Consumer in Flutter](https://stackoverflow.com/questions/58774301/when-to-use-provider-ofx-vs-consumerx-in-flutter) – Robert Sandberg Feb 14 '23 at 05:52
  • Note that context.watch() is another way to access data from a provider, which is equivalent to Provider.of(context) – Robert Sandberg Feb 14 '23 at 05:54

1 Answers1

2

They are the same. it depends on your needs.

context.watch<T>() and Consumer<T> does the same thing. Most of the time context.watch<T>() is just more convenient. In some cases where context is not available Consumer is useful.

if you want to find an answer that explained how to use provider you can use my answer.

happy coding...

Amirali Eric Janani
  • 1,477
  • 3
  • 10
  • 20