import 'dart:async'; import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter/cupertino.dart'; import 'search_repository.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; part 'search_event.dart'; part 'search_state.dart'; class SearchBloc extends Bloc { // SreachBloc() : super(SreachInitial()); SearchRepository repository; SearchBloc({this.repository}) : super(SearchInitial()); @override Stream mapEventToState( SearchEvent event, ) async* { /// 初始化方法 if (event is SearchInitEvent) { yield* _mapInitEventToState(event); } } /// 初始化 Stream _mapInitEventToState(SearchInitEvent event) async* { /// 获取缓存的数据 var cache = await repository.fetchCachedData(event.model); if(!EmptyUtil.isEmpty(cache)){ print('缓存数据'); yield SearchLoadedState(model: cache); } /// 获取数据 var result = await repository.fetchInit(event.model); Logger.log('result = ${result.toString()}'); if (!EmptyUtil.isEmpty(result)) { yield SearchLoadedState(model: result); } else { yield SearchErrorState(); } } }