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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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}) : super(TeamInitial());
  15. @override
  16. Stream<TeamState> mapEventToState(
  17. TeamEvent event,
  18. ) async* {
  19. /// 初始化
  20. if (event is TeamInitEvent) {
  21. yield* _mapInitEventToState(event);
  22. }
  23. }
  24. /// 初始化数据
  25. Stream<TeamState> _mapInitEventToState(TeamInitEvent event) async* {
  26. var cache = await repository.fetchCacheStyleData(event.data);
  27. if (!EmptyUtil.isEmpty(cache)) {
  28. yield TeamLoadedState(styleModel: cache);
  29. }
  30. var resultStyle = await repository.fetchNetStyleData(event.data);
  31. if (!EmptyUtil.isEmpty(resultStyle)) {
  32. yield TeamLoadedState(styleModel: resultStyle);
  33. }
  34. var resultData = await repository.fetchNetData();
  35. if (!EmptyUtil.isEmpty(resultData) && (!EmptyUtil.isEmpty(resultStyle) || !EmptyUtil.isEmpty(cache))) {
  36. yield TeamLoadedState(dataModel: resultData, styleModel: !EmptyUtil.isEmpty(resultStyle) ? resultStyle : cache);
  37. } else {
  38. yield TeamErrorState();
  39. }
  40. }
  41. }