import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_app/presentation/like_bloc/like_event.dart'; import 'package:flutter_app/presentation/like_bloc/like_state.dart'; import 'package:shared_preferences/shared_preferences.dart'; const String _likedPrefsKey = 'liked'; class LikeBloc extends Bloc { LikeBloc() : super(const LikeState(likedIds: [])) { on(_onChangeLike); on(_onLoadLikes); } Future _onLoadLikes(LoadLikesEvent event, Emitter emit) async { final prefs = await SharedPreferences.getInstance(); final data = prefs.getStringList(_likedPrefsKey); emit(state.copyWith(likedIds: data)); } Future _onChangeLike(ChangeLikeEvent event, Emitter emit) async { final updatedList = List.from(state.likedIds ?? []); if (updatedList.contains(event.id)) { updatedList.remove(event.id); } else { updatedList.add(event.id); } final prefs = await SharedPreferences.getInstance(); prefs.setStringList(_likedPrefsKey, updatedList); emit(state.copyWith(likedIds: updatedList)); } }