问题描述
我对这个框架很陌生,并且使用提供程序包进行状态管理,我遇到了 ChangeNotifierProvider
和 ChangeNotifierProvider.value
,但我无法区分它们的用途案例.
I am quite new to this framework and working on state management using provider package where I come across ChangeNotifierProvider
and ChangeNotifierProvider.value
, but I am unable to distinguish their use case.
我曾使用 ChangeNotifierProvider
代替 ChangeNotifierProvider.value
,但它没有按预期工作.
I had used ChangeNotifierProvider
in place of ChangeNotifierProvider.value
, but it doesn't work as intended.
推荐答案
让我们一步一步来.
扩展ChangeNotifier
的类可以调用notifyListeners()
,只要该类中的数据已更新并且您想让侦听器知道该更新.这通常在视图模型中完成,以通知 UI 根据新数据重建布局.
A class that extends ChangeNotifier
can call notifyListeners()
any time data in that class has been updated and you want to let a listener know about that update. This is often done in a view model to notify the UI to rebuild the layout based on the new data.
这是一个例子:
class MyChangeNotifier extends ChangeNotifier {
int _counter = 0;
int get counter => _counter;
void increment() {
_counter++;
notifyListeners();
}
}
ChangeNotifierProvider
是 Provider 包中的 noreferrer">多种类型的提供者.如果您已经有一个 ChangeNotifier 类(如上一个),那么您可以使用 ChangeNotifierProvider
将其提供给 UI 布局中您需要它的位置.
ChangeNotifierProvider
is one of many types of providers in the Provider package. If you already have a ChangeNotifier class (like the one above), then you can use ChangeNotifierProvider
to provide it to the place you need it in the UI layout.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MyChangeNotifier>( // define it
create: (context) => MyChangeNotifier(), // create it
child: MaterialApp(
...
child: Consumer<MyChangeNotifier>( // get it
builder: (context, myChangeNotifier, child) {
...
myChangeNotifier.increment(); // use it
特别注意,MyChangeNotifier 类的一个新实例是在这一行中创建的:
Note in particular that a new instance of the MyChangeNotifier class was created in this line:
create: (context) => MyChangeNotifier(),
这是在第一次构建小部件时完成的,而不是在后续重建时完成.
This is done one time when the widget is first built, and not on subsequent rebuilds.
如果您已经创建了 ChangeNotifier
类的实例,请使用 ChangeNotifierProvider.value
.如果您在 StatefulWidget
的 StateinitState()
方法中初始化了 ChangeNotifier
类,则可能会发生这种情况/code> 类.
Use ChangeNotifierProvider.value
if you have already created an instance of the ChangeNotifier
class. This type of situation might happen if you had initialized your ChangeNotifier
class in the initState()
method of your StatefulWidget
's State
class.
在这种情况下,您不希望创建 ChangeNotifier
的全新实例,因为您会浪费您已经完成的任何初始化工作.使用 ChangeNotifierProvider.value
构造函数允许您提供预先创建的 ChangeNotifier
值.
In that case, you wouldn't want to create a whole new instance of your ChangeNotifier
because you would be wasting any initialization work that you had already done. Using the ChangeNotifierProvider.value
constructor allows you to provide your pre-created ChangeNotifier
value.
class _MyWidgeState extends State<MyWidge> {
MyChangeNotifier myChangeNotifier;
@override
void initState() {
myChangeNotifier = MyChangeNotifier();
myChangeNotifier.doSomeInitializationWork();
super.initState();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<MyChangeNotifier>.value(
value: myChangeNotifier, // <-- important part
child: ...
特别注意这里没有create
参数,而是value
参数.这就是您传入 ChangeNotifier
类实例的地方.同样,不要尝试在那里创建新实例.
Take special note that there isn't a create
parameter here, but a value
parameter. That's where you pass in your ChangeNotifier
class instance. Again, don't try to create a new instance there.
你也可以在官方文档中找到ChangeNotifierProvider
和ChangeNotifierProvider.value
的用法:https://pub.dev/packages/provider#exposing-a-value
You can also find the usage of ChangeNotifierProvider
and ChangeNotifierProvider.value
described in the official docs: https://pub.dev/packages/provider#exposing-a-value
这篇关于ChangeNotifierProvider 与 ChangeNotifierProvider.value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!