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.
Scoped Model package usage:
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")
])
);
}
}
Provider package usage:
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")
])
);
}
}
Now assume that we have another widget that trigger the notification with increment();
of CounterModelWithChangeNotifierProvider
and CounterAppWithScopedModel
and cause the widgets to be rebuilt.
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?