import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../models/event.dart'; import '../services/api_service.dart'; class EventState { final List events; final bool isLoading; final String? error; final bool publishSuccess; const EventState({ this.events = const [], this.isLoading = false, this.error, this.publishSuccess = false, }); EventState copyWith({ List? events, bool? isLoading, String? error, bool? publishSuccess, }) { return EventState( events: events ?? this.events, isLoading: isLoading ?? this.isLoading, error: error, publishSuccess: publishSuccess ?? this.publishSuccess, ); } } class EventNotifier extends StateNotifier { EventNotifier() : super(const EventState()); Future loadEvents() async { state = state.copyWith(isLoading: true, error: null); try { final events = await apiService.getEvents(); state = state.copyWith(events: events, isLoading: false); } catch (e) { state = state.copyWith(isLoading: false, error: e.toString()); } } Future publishEvent(Map data) async { state = state.copyWith(isLoading: true, error: null, publishSuccess: false); try { final event = await apiService.publishEvent(data); state = state.copyWith( events: [event, ...state.events], isLoading: false, publishSuccess: true, ); } catch (e) { state = state.copyWith(isLoading: false, error: e.toString(), publishSuccess: false); } } void clearPublishSuccess() { state = state.copyWith(publishSuccess: false); } } final eventProvider = StateNotifierProvider((ref) { return EventNotifier(); });