何时使用Provider.of< X>与消费者X在Flutter [英] When to use Provider.of<X> vs. Consumer<X> in Flutter
问题描述
我仍然在扑朔迷离地关注状态管理技术,对于何时何地使用 Provider.of< X>
与 Consumer< X>
。我从文档了解(我认为),在这两者之间进行选择时,当我们要访问数据时可以使用Provider.of,但不需要更改UI。因此,以下内容(来自文档)可以访问数据并在发生新事件时更新UI:
return HumongousWidget(
// ...
子对象:AnotherMonstrousWidget(//<-此小部件将根据新的数据事件重建
// ...
子对象:Consumer< CartModel>(
生成器:(上下文,购物车,子项){
return Text('Total price:$ {cart.totalPrice}');
},
),
) ,
);
在这里,我们只需要数据而不希望使用UI进行重建,将 Provider.of< X>
与 listen
参数设置为 false $ c一起使用$ c>,如下所示:
Provider.of< CartModel>(上下文,监听:false).add(item); \\小部件不会重建
但是,收听
不是必需的,因此以下内容也将运行:
Provider.of< CartModel>(上下文)。新增项目); ener侦听器可选
所以这给我带来了几个问题:
- 这是区分
Provider.of< X>
和Consumer< ; X>
。前者不会更新UI,后者不会吗? - 如果
listen
未被设置为false
默认情况下会重新构建小部件还是不重新构建?如果listen
设置为true
怎么办? - 为什么要<< c $ c> Provider.of ,当我们有
Consumer
? - 在所有窗口小部件生命周期中,包括点击处理程序和
didChangeDependencies
- 不会增加缩进
- 允许重新构建更精细的小部件
- 解决了大多数BuildContext滥用问题
- Is this the correct way to distinguish
Provider.of<X>
andConsumer<X>
. Former doesn't update UI, latter does? - If
listen
isn't set tofalse
will the widget be rebuilt by default or not rebuilt? What iflisten
is set totrue
? - Why have
Provider.of
with the option to rebuild the UI at all when we haveConsumer
? - can be called in all the widgets lifecycle, including click handlers and
didChangeDependencies
- doesn't increase the indentation
- allows more granular widgets rebuilds
- solves most BuildContext misuse
时,可以选择完全重建UI。 ol>
没关系。但是要快速解释一下:
Provider.of
是唯一获取方式并听一个对象。
Consumer
, Selector
和所有* ProxyProvider调用 Provider.of
上班。
Provider.of
与 Consumer
是个人喜好问题。但这两者都有一些论点。
Provider.of
消费者
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<X>
vs. Consumer<X>
. I understand (I think) from the documentation that when choosing between these two you would use Provider.of when we want access to the data, but you don't need the UI to change. So the following (taken from the docs) gets access to the data and updates the UI on new events:
return HumongousWidget(
// ...
child: AnotherMonstrousWidget(// <- This widget will rebuild on new data events
// ...
child: Consumer<CartModel>(
builder: (context, cart, child) {
return Text('Total price: ${cart.totalPrice}');
},
),
),
);
Whereas, where we only need the data on don't want to rebuild with UI, we'd use Provider.of<X>
with the listen
parameter set to false
, as below:
Provider.of<CartModel>(context, listen: false).add(item); \\Widget won't rebuild
However, listen
isn't required and so the following will run too:
Provider.of<CartModel>(context).add(item); \\listener optional
So this brings me to a few questions:
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
Provider.of
Consumer
这篇关于何时使用Provider.of< X>与消费者X在Flutter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!