import 'dart:async'; import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:flutter/cupertino.dart'; import 'package:zhiying_base_widget/pages/team_page/bloc/team_repository.dart'; import 'package:zhiying_base_widget/pages/team_page/model/team_data_model.dart'; import 'package:zhiying_base_widget/pages/team_page/model/team_style_model.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; part 'team_event.dart'; part 'team_state.dart'; class TeamBloc extends Bloc { // TeamBloc() : super(TeamInitial()); TeamRepository repository; TeamBloc({@required this.repository}); @override TeamState get initialState => TeamInitial(); @override Stream mapEventToState( TeamEvent event, ) async* { /// 初始化 if (event is TeamInitEvent) { yield* _mapInitEventToState(event); } } /// 初始化数据 Stream _mapInitEventToState(TeamInitEvent event) async* { var cache = await repository.fetchCacheStyleData(event.data); if (!EmptyUtil.isEmpty(cache)) { yield TeamLoadedState(styleModel: cache); } var resultStyle = await repository.fetchNetStyleData(event.data); if (!EmptyUtil.isEmpty(resultStyle)) { yield TeamLoadedState(styleModel: resultStyle); } var resultData = await repository.fetchNetData(); if (!EmptyUtil.isEmpty(resultData) && (!EmptyUtil.isEmpty(resultStyle) || !EmptyUtil.isEmpty(cache))) { yield TeamLoadedState(dataModel: resultData, styleModel: !EmptyUtil.isEmpty(resultStyle) ? resultStyle : cache); } else { yield TeamErrorState(); } } }