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

team_bloc.dart 1.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. }