import 'dart:async'; import 'package:zhiying_base_widget/pages/feedback_page/bloc/feedback_repository.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; import 'feedback_event.dart'; import 'feedback_state.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class FeedbackBloc extends Bloc { // FeedbackBloc() : super(FeedbackInitial()); @override FeedbackState get initialState => FeedbackInitial(); FeedBackRepository repository; FeedbackBloc(this.repository); @override Stream mapEventToState( FeedbackEvent event, ) async* { /// 初始化 if (event is FeedbackInitEvent) { yield* _mapInitToState(event); } /// 提交反馈 if (event is FeedbackSubmitEvent) { yield* _mapSaveToState(event); } } /// 提交反馈 Stream _mapSaveToState(FeedbackSubmitEvent event) async* { var result = await repository.saveFeedback(event?.model); if (!EmptyUtil.isEmpty(result)) { yield FeedbackSaveSuccessState(); } else { yield FeedbackSaveErrorState(); } } /// 初始化 Stream _mapInitToState(FeedbackInitEvent event) async* { var cache = await repository.fetchCachedData(event.model); if (!EmptyUtil.isEmpty(cache)) { yield FeedbackLoadedState(model: cache); } var result = await repository.fetchData(event.model); if (!EmptyUtil.isEmpty(result)) { yield FeedbackLoadedState(model: result); } else { yield FeedbackErrorState(); } } }