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

52 lines
1.2 KiB

  1. import 'dart:async';
  2. import 'package:bloc/bloc.dart';
  3. import 'package:equatable/equatable.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'search_repository.dart';
  6. import 'package:zhiying_comm/zhiying_comm.dart';
  7. part 'search_event.dart';
  8. part 'search_state.dart';
  9. class SearchBloc extends Bloc<SearchEvent, SearchState> {
  10. // SreachBloc() : super(SreachInitial());
  11. SearchRepository repository;
  12. SearchBloc({this.repository}) : super(SearchInitial());
  13. @override
  14. Stream<SearchState> mapEventToState(
  15. SearchEvent event,
  16. ) async* {
  17. /// 初始化方法
  18. if (event is SearchInitEvent) {
  19. yield* _mapInitEventToState(event);
  20. }
  21. }
  22. /// 初始化
  23. Stream<SearchState> _mapInitEventToState(SearchInitEvent event) async* {
  24. /// 获取缓存的数据
  25. var cache = await repository.fetchCachedData(event.model);
  26. if(!EmptyUtil.isEmpty(cache)){
  27. print('缓存数据');
  28. yield SearchLoadedState(model: cache);
  29. }
  30. /// 获取数据
  31. var result = await repository.fetchInit(event.model);
  32. Logger.log('result = ${result.toString()}');
  33. if (!EmptyUtil.isEmpty(result)) {
  34. yield SearchLoadedState(model: result);
  35. } else {
  36. yield SearchErrorState();
  37. }
  38. }
  39. }