|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 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<FeedbackEvent, FeedbackState> {
- // FeedbackBloc() : super(FeedbackInitial());
- @override
- FeedbackState get initialState => FeedbackInitial();
-
- FeedBackRepository repository;
-
- FeedbackBloc(this.repository);
-
- @override
- Stream<FeedbackState> mapEventToState(
- FeedbackEvent event,
- ) async* {
- /// 初始化
- if (event is FeedbackInitEvent) {
- yield* _mapInitToState(event);
- }
-
- /// 提交反馈
- if (event is FeedbackSubmitEvent) {
- yield* _mapSaveToState(event);
- }
- }
-
- /// 提交反馈
- Stream<FeedbackState> _mapSaveToState(FeedbackSubmitEvent event) async* {
- var result = await repository.saveFeedback(event?.model);
- if (!EmptyUtil.isEmpty(result)) {
- yield FeedbackSaveSuccessState();
- } else {
- yield FeedbackSaveErrorState();
- }
- }
-
- /// 初始化
- Stream<FeedbackState> _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();
- }
- }
- }
|