text
stringlengths
1
372
the only required argument of the consumer widget
is the builder. builder is a function that is called whenever the
ChangeNotifier changes. (in other words, when you call notifyListeners()
in your model, all the builder methods of all the corresponding
consumer widgets are called.)
the builder is called with three arguments. the first one is context,
which you also get in every build method.
the second argument of the builder function is the instance of
the ChangeNotifier. it’s what we were asking for in the first place.
you can use the data in the model to define what the UI should look like
at any given point.
the third argument is child, which is there for optimization.
if you have a large widget subtree under your consumer
that doesn’t change when the model changes, you can construct it
once and get it through the builder.
<code_start>
return Consumer<CartModel>(
builder: (context, cart, child) => stack(
children: [
// use SomeExpensiveWidget here, without rebuilding every time.
if (child != null) child,
Text('Total price: ${cart.totalprice}'),
],
),
// build the expensive widget here.
child: const SomeExpensiveWidget(),
);
<code_end>
it is best practice to put your consumer widgets as deep in the tree
as possible. you don’t want to rebuild large portions of the UI
just because some detail somewhere changed.
<code_start>
// DON'T DO THIS
return Consumer<CartModel>(
builder: (context, cart, child) {
return HumongousWidget(
// ...
child: AnotherMonstrousWidget(
// ...
child: Text('Total price: ${cart.totalprice}'),
),
);
},
);
<code_end>
instead:
<code_start>
// DO THIS
return HumongousWidget(
// ...
child: AnotherMonstrousWidget(
// ...
child: Consumer<CartModel>(
builder: (context, cart, child) {
return Text('Total price: ${cart.totalprice}');
},
),
),
);
<code_end>
<topic_end>
<topic_start>
provider.of
sometimes, you don’t really need the data in the model to change the
UI but you still need to access it. for example, a ClearCart
button wants to allow the user to remove everything from the cart.
it doesn’t need to display the contents of the cart,
it just needs to call the clear() method.
we could use Consumer<CartModel> for this,
but that would be wasteful. we’d be asking the framework to
rebuild a widget that doesn’t need to be rebuilt.
for this use case, we can use provider.of,
with the listen parameter set to false.
<code_start>
Provider.of<CartModel>(context, listen: false).removeAll();
<code_end>
using the above line in a build method won’t cause this widget to
rebuild when notifyListeners is called.
<topic_end>
<topic_start>
putting it all together
you can check out the example covered in this article.
if you want something simpler,
see what the simple counter app looks like when
built with provider.
by following along with these articles, you’ve greatly
improved your ability to create state-based applications.
try building an application with provider yourself to
master these skills.
<topic_end>
<topic_start>
list of state management approaches
state management is a complex topic.
if you feel that some of your questions haven’t been answered,
or that the approach described on these pages
is not viable for your use cases, you are probably right.
learn more at the following links,
many of which have been contributed by the flutter community:
<topic_end>
<topic_start>