ChangeNotifierProvider

ChangeNotifierProvider

本文介绍了ChangeNotifierProvider与ChangeNotifierProvider.value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个框架很陌生,并且使用提供程序包进行状态管理,在其中遇到了ChangeNotifierProviderChangeNotifierProvider.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();
  }
}

我在构建Flutter应用程序的初学者指南.

ChangeNotifierProvider许多类型之一提供商软件包中的提供商.如果您已经有一个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.如果您已经在StatefulWidgetState类的initState()方法中初始化了ChangeNotifier类,则可能会发生这种情况.

Use ChangeNotifierProvider.value if you have already created an instance of the ChangeNotifier class. This type of situation might be 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 19:10