Solution: State Management
Explore solutions to the Riverpod challenges.
Solutions
Follow the steps below and compare your answers to each challenge you attempted in the previous lesson.
Challenge 1: Add Packages
We go to line 16 of the pubspec.yaml file and replace # TODO-1: Add flutter_riverpod package with the line below:
Challenge 2: Complete the TasksChangeNotifier class
-
We first import the
flutter_riverpodpackage (line 4). -
On line 7, we declare a
tasksProviderobject, which creates and exposes an instance ofTasksChangeNotifierto be accessed elsewhere within the application. -
Line 19 allows us to add a single task to the list of tasks given its string name. Note that after adding a new task to the list, we call the
notifyListeners()method (line 20), which notifies the users of theTasksChangeNotifierobject that the object changes. -
On line 24, we delete the task occupying the provided index and then ...