简体   繁体   中英

BlocProvider.of() called with a context that does not contain a "BlocName"

I have an issue when using MultiBlocProvider , and passing the blocContext through the widgets. I have in ShopScreen a MultiBlocProvider, and inside it a filter button, which shows a categories list from a ModalBottomSheet. like this:

modalBottomSheet

I have to update the shop screen when I choose a different category from the list. So, I have this BlocConsumer wrapped with the Filter Button, and I have to pass the context to the modalBottomSheet, so I used BlocProvider.value -which show me this error when I press on the filter button- 错误 :

My Multi Bloc Provider:

  MultiBlocProvider(
      providers: [
        BlocProvider<ProductsBloc>(create: (_) => productBloc),
        BlocProvider<CategoryBloc>(create: (_) => CategoryBloc()),
        BlocProvider<BannerBloc>(
            create: (_) => BannerBloc()..add(ShowBannerEvent())),
      ],

The FilterButton

 BlocConsumer<CategoryBloc, CategoryStates>(
                    builder: (context, cateState) {
                      return InkWell(
                        onTap: () {
                          FocusScope.of(context).unfocus();
                          showModalBottomSheet(
                              context: context,
                              builder: (context) => BlocProvider.value(
                                  value:
                                      BlocProvider.of<CategoryBloc>(context)
                                        ..add(ShowCategoryEvent()),
                                  child: CategoryBottomSheet()),
                              isScrollControlled: true);
                        },
                        child: SizedBox(
                          height: 50.h,
                          width: 50.w,
                          child: SvgPicture.asset(
                            'assets/fillter_button.svg', ),),);
                    },
                    listener: (context, cateState) {
                      if (cateState is CategorySelectedState)
                        widget.categoryId = cateState.categoryId;
                    },
                  ),

Is there something missing?

If you want to access a bloc, you should use context.read<CategoryBloc>() .

So for example:

context.read<CategoryBloc>().add(ShowCategoryEvent());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM