基础组件库
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

56 wiersze
1.5 KiB

  1. import 'dart:async';
  2. import 'package:zhiying_base_widget/pages/feedback_page/bloc/feedback_repository.dart';
  3. import 'package:zhiying_comm/zhiying_comm.dart';
  4. import 'feedback_event.dart';
  5. import 'feedback_state.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. class FeedbackBloc extends Bloc<FeedbackEvent, FeedbackState> {
  8. // FeedbackBloc() : super(FeedbackInitial());
  9. @override
  10. FeedbackState get initialState => FeedbackInitial();
  11. FeedBackRepository repository;
  12. FeedbackBloc(this.repository);
  13. @override
  14. Stream<FeedbackState> mapEventToState(
  15. FeedbackEvent event,
  16. ) async* {
  17. /// 初始化
  18. if (event is FeedbackInitEvent) {
  19. yield* _mapInitToState(event);
  20. }
  21. /// 提交反馈
  22. if (event is FeedbackSubmitEvent) {
  23. yield* _mapSaveToState(event);
  24. }
  25. }
  26. /// 提交反馈
  27. Stream<FeedbackState> _mapSaveToState(FeedbackSubmitEvent event) async* {
  28. var result = await repository.saveFeedback(event?.model);
  29. if (!EmptyUtil.isEmpty(result)) {
  30. yield FeedbackSaveSuccessState();
  31. } else {
  32. yield FeedbackSaveErrorState();
  33. }
  34. }
  35. /// 初始化
  36. Stream<FeedbackState> _mapInitToState(FeedbackInitEvent event) async* {
  37. var cache = await repository.fetchCachedData(event.model);
  38. if (!EmptyUtil.isEmpty(cache)) {
  39. yield FeedbackLoadedState(model: cache);
  40. }
  41. var result = await repository.fetchData(event.model);
  42. if (!EmptyUtil.isEmpty(result)) {
  43. yield FeedbackLoadedState(model: result);
  44. } else {
  45. yield FeedbackErrorState();
  46. }
  47. }
  48. }