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

search_bloc.dart 1.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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});
  13. @override
  14. SearchState get initialState => SearchInitial();
  15. @override
  16. Stream<SearchState> mapEventToState(
  17. SearchEvent event,
  18. ) async* {
  19. /// 初始化方法
  20. if (event is SearchInitEvent) {
  21. yield* _mapInitEventToState(event);
  22. }
  23. /// 搜索的方法
  24. if (event is SearchSubmitEvent) {
  25. yield* _mapSearchSubmitToState(event);
  26. }
  27. }
  28. /// 初始化
  29. Stream<SearchState> _mapInitEventToState(SearchInitEvent event) async* {
  30. /// 获取缓存的数据
  31. var cache = await repository.fetchCachedData(event.model);
  32. if(!EmptyUtil.isEmpty(cache)){
  33. print('缓存数据');
  34. yield SearchLoadedState(model: cache);
  35. }
  36. /// 获取数据
  37. var result = await repository.fetchInit(event.model);
  38. Logger.log('result = ${result.toString()}');
  39. if (!EmptyUtil.isEmpty(result)) {
  40. yield SearchLoadedState(model: result);
  41. } else {
  42. yield SearchErrorState();
  43. }
  44. }
  45. /// 搜索的方法
  46. Stream<SearchState> _mapSearchSubmitToState(SearchSubmitEvent event) async* {
  47. var result = await repository.fetchSearchSubmit(event.model);
  48. }
  49. }