text stringlengths 1 474 |
|---|
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> |
General overview |
Things to review before selecting an approach.<topic_end> |
<topic_start> |
Provider |
<topic_end> |
<topic_start> |
Riverpod |
Riverpod works in a similar fashion to Provider. |
It offers compile safety and testing without depending on the Flutter SDK.<topic_end> |
<topic_start> |
setState |
The low-level approach to use for widget-specific, ephemeral state.<topic_end> |
<topic_start> |
ValueNotifier & InheritedNotifier |
An approach using only Flutter provided tooling to update state and notify the UI of changes.<topic_end> |
<topic_start> |
InheritedWidget & InheritedModel |
The low-level approach used to communicate between ancestors and children |
in the widget tree. This is what provider and many other approaches |
use under the hood.The following instructor-led video workshop covers how to |
use InheritedWidget:Other useful docs include:<topic_end> |
<topic_start> |
June |
A lightweight and modern state management library that focuses on providing |
a pattern similar to Flutter’s built-in state management.<topic_end> |
<topic_start> |
Redux |
A state container approach familiar to many web developers.<topic_end> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.