Flutter TabBar 错误状态:调用 close 后无法添加新事件

Flutter TabBar Bad state: Cannot add new events after calling close

提问人:Pers 提问时间:11/14/2023 更新时间:11/14/2023 访问量:16

问:

我的肘被关闭有问题,所以我无法向它发出新的状态。切换选项卡时有时会发生这种情况。 我的根屏幕是一个无状态的小部件。

class RootScreen extends StatelessWidget {
  const RootScreen({super.key, required this.selectedTab});

  final RootScreenState selectedTab;

  @override
  Widget build(BuildContext context) {
    return BlocProvider<RootCubit>(
      create: (_) => Injector.locateService<RootCubit>(),
      child: _RootView(
        selectedTab: selectedTab,
      ),
    );
  }
}

RootView 是一个像这样的有状态小部件

class _RootViewState extends State<_RootView>
    with SingleTickerProviderStateMixin {
    }

在其中,我有一个有 5 个子项的 tabbarview,每个子项都由 blocbuilder 提供,如下所示。

TabBarView(
                physics: const NeverScrollableScrollPhysics(),
                controller: tabController,
                children: [
                  BlocBuilder<RootCubit, RootState>(
                    builder: (context, state) {
                      return getTabBarViewContent();
                    },
                  ),
                  BlocBuilder<RootCubit, RootState>(
                    builder: (context, state) {
                      return getTabBarViewContent();
                    },
                  ),
                  BlocBuilder<RootCubit, RootState>(
                    builder: (context, state) {
                      return getTabBarViewContent();
                    },
                  ),
                  BlocBuilder<RootCubit, RootState>(
                    builder: (context, state) {
                      return getTabBarViewContent();
                    },
                  ),
                  BlocBuilder<RootCubit, RootState>(
                    builder: (context, state) {
                      return getTabBarViewContent();
                    },
                  ),
                ],
              ),

现在,有时当我切换选项卡时,会出现错误状态错误。我所有的选项卡视图都有自己的 bloc provider 和 cubit,如下所示

class SomeScreen extends StatelessWidget {
  const SomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider<SomeCubit>(
      create: (_) => Injector.locateService<SomeScreenCubit>(),
      child: SomeScreenView(),
    );
  }
}

其中 somescreen 是一个 statefull 小部件,其状态如下所示

class SomeViewState extends State<SomeView>
    with AutomaticKeepAliveClientMixin<SomeView> {
  late SomeCubit _cubit;

  @override
  void initState() {
    super.initState();
    _cubit = context.read<SomeCubit>();
    _cubit.loadData();
  }



     @override
      bool get wantKeepAlive => true;


 @override
  Widget build(BuildContext context) {
    super.build(context);
    //etc}

}

最后,我以肘为单位加载这样的数据,这发出数据线崩溃了。知道为什么会发生这种情况吗?

@Injectable()
class SomeCubit extends Cubit<SomeCubitState> {
  SomeCubit ({required this.repository})
      : super(const SomeScreenLoading());

  final IRepository repository;

  Future<void> loadData() async {
    var acccountInfo = await repository.getAccountInformation();
    if (acccountInfo == null) {
      emit(const SomeDataLoaded());
    } else {
      emit(SomeDataLoaded(accountInfo: accountInfo));
    }
  }

我知道加载的是好的,我只是在捕获未捕获的异常时捕获这些错误,并且不知道这些问题是否会在生产中持续存在,所以我想解决它们。只需在断点上保持未捕获的异常处于未选中状态即可解决问题,并且无需调试代码即可正常工作,就像按 contiune 时一样。

颤振

评论


答: 暂无答案