基础库
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.
 
 
 
 
 

39 lines
1.0 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. EmptyPageRepository repository;
  9. EmptyPageBloc(this.repository) : super(EmptyPageInitial());
  10. @override
  11. Stream<EmptyPageState> mapEventToState(
  12. EmptyPageEvent event,
  13. ) async* {
  14. if (event is EmptyPageInitEvent) {
  15. yield* _mapInitEventToState(event);
  16. }
  17. }
  18. /// 初始化
  19. Stream<EmptyPageState> _mapInitEventToState(EmptyPageInitEvent event) async* {
  20. var cache = await repository.fetchCacheData();
  21. if (!EmptyUtil.isEmpty(cache)) {
  22. yield EmptyPageLoadedState(model: cache);
  23. }
  24. var result = await repository.fetchNetData();
  25. if (!EmptyUtil.isEmpty(result)) {
  26. yield EmptyPageLoadedState(model: result);
  27. } else {
  28. yield EmptyPageErrorState();
  29. }
  30. }
  31. }