Skip to content

Commit

Permalink
favorites_provider.dart is added.
Browse files Browse the repository at this point in the history
  • Loading branch information
musamairshad committed Jun 30, 2023
1 parent b51a615 commit 1a052ae
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/providers/favorites_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 1,21 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:meals_app/models/meal.dart';

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();
});

0 comments on commit 1a052ae

Please sign in to comment.