Solution: State Management
Explore how to manage app state effectively in Flutter by implementing and using the flutter_riverpod package. Learn to update, add, and delete tasks while ensuring UI reflects these changes in real-time. This lesson guides you through practical examples to help you build responsive and scalable Flutter apps.
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 ...