Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Provider package in a Flutter app.
Flutter
import 'package:flutter/material.dart'; import '[1]';
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to include 'package:' in the import path.
Using incorrect folder names in the import path.
✗ Incorrect
The correct import path for the Provider package is 'package:provider/provider.dart'.
2fill in blank
mediumComplete the code to provide a ChangeNotifier to the widget tree.
Flutter
ChangeNotifierProvider(
create: (context) => [1](),
child: MyApp(),
) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ChangeNotifier' instead of your model class name.
Passing BuildContext instead of a model instance.
✗ Incorrect
You provide an instance of your ChangeNotifier class, here named MyModel, to the widget tree.
3fill in blank
hardFix the error in accessing the provider inside a widget build method.
Flutter
final model = Provider.of<[1]>(context); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using BuildContext or Widget as the generic type.
Omitting the generic type entirely.
✗ Incorrect
You must specify your model class (e.g., MyModel) inside the angle brackets to access the provider.
4fill in blank
hardFill both blanks to listen to changes and rebuild the widget accordingly.
Flutter
final model = Provider.of<[1]>(context, [2]: true);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'child' or 'context' instead of 'listen' as the parameter.
Setting listen to false when you want to rebuild on changes.
✗ Incorrect
To listen for changes and rebuild, set listen: true when accessing the provider with your model class.
5fill in blank
hardFill all three blanks to update the model and notify listeners.
Flutter
class MyModel extends ChangeNotifier { int _count = 0; int get count => [1]; void increment() { _count [2] 1; [3](); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 'count' inside the getter causing recursion.
Using '=' instead of '+=' for increment.
Forgetting to call notifyListeners() after changing state.
✗ Incorrect
The getter returns _count, increment adds 1 to _count, and notifyListeners() tells widgets to rebuild.