基础库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

40 line
1.1 KiB

  1. import 'dart:async';
  2. import 'package:bloc/bloc.dart';
  3. import 'package:zhiying_comm/pages/empty_page/bloc/empty_page_repository.dart';
  4. import 'package:zhiying_comm/util/empty_util.dart';
  5. import 'empty_page_state.dart';
  6. import 'empty_page_event.dart';
  7. class EmptyPageBloc extends Bloc<EmptyPageEvent, EmptyPageState> {
  8. @override
  9. EmptyPageState get initialState => EmptyPageInitial();
  10. EmptyPageRepository repository;
  11. EmptyPageBloc(this.repository);
  12. @override
  13. Stream<EmptyPageState> mapEventToState(
  14. EmptyPageEvent event,
  15. ) async* {
  16. if (event is EmptyPageInitEvent) {
  17. yield* _mapInitEventToState(event);
  18. }
  19. }
  20. /// 初始化
  21. Stream<EmptyPageState> _mapInitEventToState(EmptyPageInitEvent event) async* {
  22. var cache = await repository.fetchCacheData();
  23. if (!EmptyUtil.isEmpty(cache)) {
  24. yield EmptyPageLoadedState(model: cache);
  25. }
  26. var result = await repository.fetchNetData();
  27. if (!EmptyUtil.isEmpty(result)) {
  28. yield EmptyPageLoadedState(model: result);
  29. } else {
  30. yield EmptyPageErrorState();
  31. }
  32. }
  33. }