Complete the code to create a ChangeNotifier class named Counter.
class Counter extends ChangeNotifier { int _count = 0; int get count => _count; void increment() { _count++; [1]; } }
In a ChangeNotifier, notifyListeners() tells the app to update widgets listening to this model.
Complete the code to use Consumer widget to listen to Counter and show the count.
Consumer<Counter>(
builder: (context, counter, child) {
return Text('Count: ${counter.[1]');
},
)The Counter class has a getter named count that holds the current count value.
Fix the error in the code to provide Counter to the widget tree.
ChangeNotifierProvider(
create: (context) => [1](),
child: MyApp(),
)The provider needs to create an instance of the Counter class, which extends ChangeNotifier.
Fill both blanks to correctly increment the counter when a button is pressed.
ElevatedButton(
onPressed: () {
context.[1]<Counter>().[2]();
},
child: Text('Increment'),
)Use context.read<Counter>() to get the model without listening, then call increment() to update the count.
Fill all three blanks to create a Consumer that rebuilds only the Text widget when count changes.
Consumer<Counter>( builder: (context, [1], [2]) { return Text('Value: $[1].[3]'); }, )
The builder function receives counter as the model, child as a widget that does not rebuild, and uses counter.count to show the value.