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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 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. /// 搜索的方法
  40. Stream<SearchState> _mapSearchSubmitToState(SearchSubmitEvent event) async* {
  41. var result = await repository.fetchSearchSubmit(event.model);
  42. }
  43. }