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

43 views Asked by At

I have an issue with my cubit being closed so I cant emit new states to it. That happens sometimes when switching tabs. I have root screen that is a stateless widget.

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 is a statefull widget like this

class _RootViewState extends State<_RootView>
    with SingleTickerProviderStateMixin {
    }

In it i have a tabbarview that has 5 children, each of it being provided by blocbuilder like this.

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();
                    },
                  ),
                ],
              ),

Now, sometimes when I switch tabs I get the bad state error. All my tabviews have their own bloc provider and cubit, like this

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

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

where somescreen is a statefull widget, and its state looks like this

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}

}

Finally, I load the data like this in cubit and this emit data line crashes. Any ideas why its happening?

@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));
    }
  }

I know that that that is loaded is good, i just catch these errors i vscode when I catch uncaught exceptions and don't know if these issues will persist in production so I want to sort them out. Just keeping uncaught exceptions unchecked on breakpoints will resolve the issue and without debugging code is working normally, as it does when pressing contiune.

0

There are 0 answers