|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 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());
-
- FeedBackRepository repository;
-
- FeedbackBloc(this.repository) : super(FeedbackInitial());
-
- @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();
- }
- }
- }
|