mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
Provider counter update (#520)
* Upgrade provider * Update maintenance table
This commit is contained in:
@@ -75,17 +75,26 @@ class MyHomePage extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
// Provider.of is another way to access the model object held
|
||||
// by an ancestor Provider. By default, even this listens to
|
||||
// changes in the model, and rebuilds the whole encompassing widget
|
||||
// when notified.
|
||||
//
|
||||
// By using `listen: false` below, we are disabling that
|
||||
// behavior. We are only calling a function here, and so we don't care
|
||||
// about the current value. Without `listen: false`, we'd be rebuilding
|
||||
// the whole MyHomePage whenever Counter notifies listeners.
|
||||
onPressed: () =>
|
||||
Provider.of<Counter>(context, listen: false).increment(),
|
||||
onPressed: () {
|
||||
// You can access your providers anywhere you have access
|
||||
// to the context. One way is to use Provider<Counter>.of(context).
|
||||
//
|
||||
// The provider package also defines extension methods on context
|
||||
// itself. You can call context.watch<Counter>() in a build method
|
||||
// of any widget to access the current state of Counter, and to ask
|
||||
// Flutter to rebuild your widget anytime Counter changes.
|
||||
//
|
||||
// You can't use context.watch() outside build methods, because that
|
||||
// often leads to subtle bugs. Instead, you should use
|
||||
// context.read<Counter>(), which gets the current state
|
||||
// but doesn't ask Flutter for future rebuilds.
|
||||
//
|
||||
// Since we're in a callback that will be called whenever the user
|
||||
// taps the FloatingActionButton, we are not in the build method here.
|
||||
// We should use context.read().
|
||||
var counter = context.read<Counter>();
|
||||
counter.increment();
|
||||
},
|
||||
tooltip: 'Increment',
|
||||
child: Icon(Icons.add),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user