I\'m still wrapping my head around state-management techniques in flutter and am a bit confused about when and why to use Provider.of
vs. Consume
The widget Consumer
doesn't do any fancy work. It just calls Provider.of
in a new widget, and delegate its build implementation to [builder].
It's just syntactic sugar for Provider.of
but the funny thing is I think Provider.of
is simpler to use.
Look at this article for more clearance https://blog.codemagic.io/flutter-tutorial-provider/
It doesn't matter. But to explain things rapidly:
Provider.of
is the only way to obtain and listen to an object.
Consumer
, Selector
, and all the *ProxyProvider calls Provider.of
to work.
Provider.of
vs Consumer
is a matter of personal preference. But there's a few arguments for both
didChangeDependencies
There should not be any performance concern by using it, moreover, we should use consumers if we want to change some specific widget only on screen. This is the best approach I can say in terms of coding practice.
return Container(
// ...
child: Consumer<PersonModel>(
builder: (context, person, child) {
return Text('Name: ${person.name}');
},
),
);
Like in the above example, we only required to update the value of Single Text Widget so add consumer there instead of Provider which is accessible to others widget as well.
Note: Consumer or Provider update the only reference of your instance which widgets are using, if some widgets are not using then it will not re-drawn.
For your questions:
Provider.of<X>
and Consumer<X>
. Former doesn't update UI, latter does?Provider.of<X>
depends on value of listen
to trigger a new State.build
to widgets and State.didChangeDependencies
for StatefulWidget
.
Consumer<X>
always update UI, as it uses Provider.of<T>(context)
, where listen
is true
. See full source here.
listen
isn't set to false
will the widget be rebuilt by default or not rebuilt? What if listen
is set to true
?Default value is true
, means will trigger a new State.build
to widgets and State.didChangeDependencies
for StatefulWidget
. See full source here.
static T of<T>(BuildContext context, {bool listen = true})
.
Provider.of
with the option to rebuild the UI at all when we have Consumer
?Pretty much covered by Rémi Rousselet's answer.