0

我正在做一个颤振项目,我正在使用 bloc 提供程序调用 Api 并在我得到输出时发出一个状态。当我以 cubit 打印输出时它的工作并且在发出该输出后我无法在 bloc builder 中得到它,甚至状态也没有在 bloc builder 中打印。我的 api 获取代码如下

Map<String, dynamic> fields = {'upload_id': widget.content.id};
          ApiModel apiData = (ApiModel(
            fields: fields,
            token: userToken == null ? userDataGlobal['data'].token : userToken,
          ));

          if (widget.content.isSeries) {
            BlocProvider.of<SeasonCountCubit>(context).getSeasonCount(apiData);
            BlocBuilder<SeasonCountCubit, SeasonCountState>(
                builder: (context, state) {
              print('state is $state');
              if (state is SeasonCountLoaded) {
                print('season Cout is ${(state as SeasonCountLoaded).data}');
                setState(() {
                 int seasonCount = (state as SeasonCountLoaded).data;
                });
              }
              return CircularProgressIndicator();
            });
          }

我的肘如下

part 'season_count_state.dart';

class SeasonCountCubit extends Cubit<SeasonCountState> {
  final Repository repository;
  SeasonCountCubit({this.repository}) : super(SeasonCountInitial());

  getSeasonCount(ApiModel fields) {
    repository.getSeasonCount(fields).then((datas) {
       emit(SeasonCountLoading());
      print('seson count in cubit $datas');
      seasonCount = datas;
      emit(SeasonCountLoaded(data: datas));
    });
  }
}
part of 'season_count_cubit.dart';

@immutable
abstract class SeasonCountState {}

class SeasonCountInitial extends SeasonCountState {}

class SeasonCountLoading extends SeasonCountState {}

class SeasonCountLoaded extends SeasonCountState {
  final int data;
  SeasonCountLoaded({this.data});
}

任何人都请帮我解决这个问题..

4

0 回答 0