Flutter 颤振中ChangeNotifierProvider和ScopedModel之间的差异
我想解释一下(使用and)和FLATTER中的区别 在研究了这两种管理应用程序状态的方法之后,我迷失了方向,因为我没有发现在代码编写方法上有任何实质性的差异 范围模型包用法:Flutter 颤振中ChangeNotifierProvider和ScopedModel之间的差异,flutter,dart,Flutter,Dart,我想解释一下(使用and)和FLATTER中的区别 在研究了这两种管理应用程序状态的方法之后,我迷失了方向,因为我没有发现在代码编写方法上有任何实质性的差异 范围模型包用法: class CounterModelWithScopedModel extends Model { int _counter = 0; int get counter => _counter; void increment() { _counter++; notifyListeners(
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")
])
);
}
}
class CounterModelWithScopedModel扩展了模型{
int _计数器=0;
int get counter=>\u计数器;
无效增量(){
_计数器++;
notifyListeners();
}
}
类CounterAppWithScopedModel扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新的ScopedModel(
模型:新的CounterModelWithScopedModel(),
子项:新列(子项:[
新ScopedModelDescentant(
生成器:(上下文、子对象、模型)=>新文本(“${model.counter}”),
),
新文本(“另一个不需要作用域模型的小部件”)
])
);
}
}
提供商软件包用法:
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")
])
);
}
}
class CounterModelWithChangeNotifierProvider扩展了ChangeNotifier{
int _计数器=0;
int get counter=>\u计数器;
无效增量(){
_计数器++;
notifyListeners();
}
}
类CounterAppWithChangeNotifierProvider扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新的ChangeNotifierProvider(
生成器:(上下文)=>CounterModelWithChangeNotifierProvider(),
子项:新列(子项:[
新消费者(
生成器:(上下文、模型、子项)=>新文本(“${model.counter}”)
),
新文本(“另一个不需要消费的小部件”)
])
);
}
}
现在假设我们有另一个小部件,它通过increment()触发通知使用ChangeNotifierProvider的CounterModel
和CounterAppWithScopedModel
的代码>并使小部件重新生成
最近我接触了flifter,对应用程序状态的管理感到非常困惑,我从Notifier开始,但看到有无数种方法可以实现,我不知道该怎么做。你推荐什么?TD;博士:
provider
不是scoped_模型
,但可用于模拟scoped_模型
体系结构
scoped_model
是一种基于Listenable
:model
子类的体系结构,该子类现在以ChangeNotifier
的名义内置于flatter中
provider
不是一种体系结构,而是一种传递状态并对其进行管理的手段。
它可以用来创建一个类似于范围的\u模型的体系结构,但它也可以做其他事情。提供程序是一个完整的依赖项注入解决方案,有一些非常强大的提供程序类型,可以处理诸如管理流之类的样板文件。然而,ScopedModel使用Model类+notifyListeners()在反应性方面提供了相同的功能。“但它也可以做其他事情”,比如什么?@Wecherowski,它有点广泛。但例如,库集团依赖于提供者。Mobx也使用它。但它们都与作用域_模型完全无关