Bloc: Modal bottom sheet and BlocProvider not found

Created on 29 Jun 2020  路  1Comment  路  Source: felangel/bloc

Describe the bug
The following error is shown when the below code is ran:
BlocProvider.of() called with a context that does not contain a Bloc.

To replicate

      BlocProvider(
          create: (context) => getIt<TheBloc>()
          child: BlocBuilder<TheBloc, TheState>(
          build: (context, state) =>
          MaterialButton(
            onPressed: () => _showModal(context),
            child: const Text('SHOW BLOC MODAL'),
          ),
void _showModal(BuildContext context) {
  showModalBottomSheet<void>(
    context: context,
    builder: (_) {
          return MaterialButton(
               onPressed() {
                       context.bloc<TheBloc>().add(
                         TheEvent.someEvent(),
                       );
               }
              child: Text('Press button to add event to bloc')
          );
    },
  );
}
question

Most helpful comment

Hi @gerryau 馃憢
Thanks for opening an issue!

You need to wrap the modal in a BlocProvider.value in order to make the bloc available to the modal content. The same applies when pushing a new route.

You can check out the bloc access recipe for more details 馃憤

>All comments

Hi @gerryau 馃憢
Thanks for opening an issue!

You need to wrap the modal in a BlocProvider.value in order to make the bloc available to the modal content. The same applies when pushing a new route.

You can check out the bloc access recipe for more details 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nerder picture nerder  路  3Comments

nhwilly picture nhwilly  路  3Comments

Reidond picture Reidond  路  3Comments

timtraversy picture timtraversy  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments