Flutter中的ChangeNotifierProvider和ScopedModel之间的区别 [英] Difference between ChangeNotifierProvider and ScopedModel in Flutter

查看:440
本文介绍了Flutter中的ChangeNotifierProvider和ScopedModel之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对提供程序包(使用 ChangeNotifier ChangeNotifierProvider )和 Flutter中的范围模型包

I wanted to have an explanation of the difference between Provider package(with usage of ChangeNotifier and ChangeNotifierProvider) and Scoped Model package in Flutter.

查看了两种管理应用程序状态的方法后,我迷路了,因为我发现代码编写方法没有任何实质性的区别。

After looking at these two methods of managing the state of the application, I was lost because I didn't find any substantial differences in the approach to code writing.

作用域模型包用法:

class CounterModelWithScopedModel extends Model {
  int _counter = 0;
  int get counter => _counter;

  void increment() {
    _counter++;
    notifyListeners();
  }
}

class CounterAppWithScopedModel extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new ScopedModel<CounterModelWithScopedModel>(
      model: new CounterModelWithScopedModel(),
      child: new Column(children: [
        new ScopedModelDescendant<CounterModelWithScopedModel>(
          builder: (context, child, model) => new Text('${model.counter}'),
        ),
        new Text("Another widget that doesn't require scoped model")
      ])
    );
  }
}

提供程序包的使用:

class CounterModelWithChangeNotifierProvider extends ChangeNotifier {
  int _counter = 0;
  int get counter => _counter;

  void increment() {
    _counter++;
    notifyListeners();
  }
}

class CounterAppWithChangeNotifierProvider extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new ChangeNotifierProvider(
      builder: (context) => CounterModelWithChangeNotifierProvider(),
      child: new Column(children: [
        new Consumer<CounterModelWithChangeNotifierProvider>(
          builder: (context, model, child) => new Text('${model.counter}')
        ),
        new Text("Another widget that doesn't require consume")
      ])
    );
  }
}

现在假定我们有另一个触发通知的小部件与 increment(); CounterModelWithChangeNotifierProvider CounterAppWithScopedModel 并导致

Now assume that we have another widget that trigger the notification with increment(); of CounterModelWithChangeNotifierProvider and CounterAppWithScopedModel and cause the widgets to be rebuilt.

我最近遇到了麻烦,对于应用程序状态的管理我感到很困惑,我从Notifier开始,但在看到有一个我不知道该怎么做的无数种方法。您有什么建议?

I have approached flutter recently and I am quite confused about the management of the application state, I started with Notifier but after seeing that there are an infinite number of ways for do that I do not know what to do. What do you recommend?

推荐答案

TD; DR:

provider 不是 scoped_model ,但可用于模仿 scoped_model

provider is not scoped_model but can be used to mimic a scoped_model architecture.

scoped_model 是一种基于子类的体系结构 Listenable 的类型: Model ,它现在是内置的Flutter,名称为 ChangeNotifier

scoped_model is an architecture, based on a subclass of Listenable: Model, that is now built-in Flutter under the name of ChangeNotifier

提供者不是体系结构,而是传递状态并对其进行管理的手段。
可以用于制作类似于 scoped_model 的体系结构,但是它也可以做其他事情

provider is not an architecture, but instead a mean to pass state and manage it. It can be used to make a scoped_model-like architecture, but it can do something else too.

这篇关于Flutter中的ChangeNotifierProvider和ScopedModel之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆