基础组件库
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

58 linhas
1.6 KiB

  1. import 'dart:async';
  2. import 'package:bloc/bloc.dart';
  3. import 'package:equatable/equatable.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:zhiying_base_widget/pages/team_page/bloc/team_repository.dart';
  6. import 'package:zhiying_base_widget/pages/team_page/model/team_data_model.dart';
  7. import 'package:zhiying_base_widget/pages/team_page/model/team_style_model.dart';
  8. import 'package:zhiying_comm/zhiying_comm.dart';
  9. part 'team_event.dart';
  10. part 'team_state.dart';
  11. class TeamBloc extends Bloc<TeamEvent, TeamState> {
  12. // TeamBloc() : super(TeamInitial());
  13. TeamRepository repository;
  14. TeamBloc({@required this.repository});
  15. @override
  16. TeamState get initialState => TeamInitial();
  17. @override
  18. Stream<TeamState> mapEventToState(
  19. TeamEvent event,
  20. ) async* {
  21. /// 初始化
  22. if (event is TeamInitEvent) {
  23. yield* _mapInitEventToState(event);
  24. }
  25. }
  26. /// 初始化数据
  27. Stream<TeamState> _mapInitEventToState(TeamInitEvent event) async* {
  28. var cache = await repository.fetchCacheStyleData(event.data);
  29. if (!EmptyUtil.isEmpty(cache)) {
  30. yield TeamLoadedState(styleModel: cache);
  31. }
  32. var resultStyle = await repository.fetchNetStyleData(event.data);
  33. if (!EmptyUtil.isEmpty(resultStyle)) {
  34. yield TeamLoadedState(styleModel: resultStyle);
  35. }
  36. var resultData = await repository.fetchNetData();
  37. if (!EmptyUtil.isEmpty(resultData) && (!EmptyUtil.isEmpty(resultStyle) || !EmptyUtil.isEmpty(cache))) {
  38. yield TeamLoadedState(dataModel: resultData, styleModel: !EmptyUtil.isEmpty(resultStyle) ? resultStyle : cache);
  39. } else {
  40. yield TeamErrorState();
  41. }
  42. }
  43. }