Skip to content

Riverpod StateNotifier is deprecated #22

@0xC0D1F1ED

Description

@0xC0D1F1ED

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions