// import 'dart:async'; import 'package:bytedesk_kefu/blocs/leavemsg_bloc/bloc.dart'; import 'package:bloc/bloc.dart'; import 'package:bytedesk_kefu/repositories/leavemsg_repository.dart'; class LeaveMsgBloc extends Bloc { // final LeaveMsgRepository leaveMsgRepository = new LeaveMsgRepository(); LeaveMsgBloc() : super(new UnLeaveMsgState()) { on(_mapSubmitLeaveMsgToState); on(_mapUploadImageToState); } // @override // Stream mapEventToState( // LeaveMsgEvent event, // ) async* { // if (event is SubmitLeaveMsgEvent) { // yield* _mapSubmitLeaveMsgToState(event); // } else if (event is UploadImageEvent) { // yield* _mapUploadImageToState(event); // } // } void _mapSubmitLeaveMsgToState( SubmitLeaveMsgEvent event, Emitter emit) async { emit(LeaveMsgSubmiting()); try { // final JsonResult jsonResult = await leaveMsgRepository.submitLeaveMsg(event.content, event.imageUrls); emit(LeaveMsgSubmitSuccessState()); } catch (error) { print(error); emit(LeaveMsgSubmitError()); } } void _mapUploadImageToState(UploadImageEvent event, Emitter emit) async { emit(ImageUploading()); try { final String url = await leaveMsgRepository.upload(event.filePath); emit(UploadImageSuccess(url)); } catch (error) { print(error); emit(UpLoadImageError()); } } }