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

search_bloc.dart 1.2 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. /// 初始化
  25. Stream<SearchState> _mapInitEventToState(SearchInitEvent event) async* {
  26. /// 获取缓存的数据
  27. var cache = await repository.fetchCachedData(event.model);
  28. if(!EmptyUtil.isEmpty(cache)){
  29. print('缓存数据');
  30. yield SearchLoadedState(model: cache);
  31. }
  32. /// 获取数据
  33. var result = await repository.fetchInit(event.model);
  34. Logger.log('result = ${result.toString()}');
  35. if (!EmptyUtil.isEmpty(result)) {
  36. yield SearchLoadedState(model: result);
  37. } else {
  38. yield SearchErrorState();
  39. }
  40. }
  41. }