-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
For anyone running into issues w/ using riverpod in this course I just wanted to post quickly to say that StateNotifier has been deprecated and the new classes are Notifier and AsyncNotifier. Here's an example of the favorites provider class supported by both the old version and the new version for comparison to anyone who might run into this going forward:
Old version (in the course videos):
class FavoriteMealsNotifier extends StateNotifier<List<Meal>> {
FavoriteMealsNotifier() : super([]);
void toggleMealFavoriteStatus(Meal meal) {
final mealIsFavorite = state.contains(meal);
if (mealIsFavorite) {
state = state.where((m) => m.id != meal.id).toList();
} else {
state = [...state, meal];
}
}
}
final favoriteMealsProvider = StateNotifierProvider<FavoriteMealsNotifier, List<Meal>>((ref) {
return FavoriteMealsNotifier();
});
New version:
final favoriteMealsProvider = NotifierProvider<FavoriteMealsNotifier, List<Meal>>(FavoriteMealsNotifier.new);
class FavoriteMealsNotifier extends Notifier<List<Meal>> {
@override
List<Meal> build() {
return [];
}
bool toggleMealFavoriteStatus(Meal meal) {
final mealIsFavorite = state.contains(meal);
if (mealIsFavorite) {
state = state.where((m) => m.id != meal.id).toList();
return false;
} else {
state = [...state, meal];
return true;
}
}
}
Metadata
Metadata
Assignees
Labels
No labels