简体   繁体   English

BlocProvider.of() 使用不包含“BlocName”的上下文调用

[英]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.我在使用MultiBlocProvider并通过小部件传递 blocContext 时遇到问题。 I have in ShopScreen a MultiBlocProvider, and inside it a filter button, which shows a categories list from a ModalBottomSheet.我在 ShopScreen 中有一个 MultiBlocProvider,里面有一个过滤器按钮,它显示来自 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-所以,我用过滤器按钮包装了这个 BlocConsumer,我必须将上下文传递给 modalBottomSheet,所以我使用了 BlocProvider.value - 当我按下过滤器按钮时会显示这个错误 - 错误 :

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>() .如果你想访问一个区块,你应该使用context.read<CategoryBloc>()

So for example:例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用不包含 RegisterCubit 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a RegisterCubit BlocProvider.of() 使用不包含 Bloc 的上下文调用 - 即使它包含 - BlocProvider.of() called with a context that does not contain a Bloc - even that it does BlocProvider.of() 使用不包含 TrackingBloc 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type TrackingBloc 使用不包含 FicheMvtBloc 类型的 Bloc 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type FicheMvtBloc BlocProvider.of() 使用不包含 Cubit 类型的上下文调用 - BlocProvider.of() called with a context that does not contain a Cubit of type BlocProvider.of() 使用不包含 CLASS 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type CLASS 使用不包含 Bloc 类型的上下文调用 Flutter BlocProvider.of() - Flutter BlocProvider.of() called with a context that does not contain a Bloc of type 使用不包含 TaharatBloc 类型的 Bloc 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type TaharatBloc BlocProvider.of() 调用的上下文不包含 MyBloc 类型的 Bloc/Cubit - BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type MyBloc 使用不包含 MainBloc 类型的 Bloc 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type MainBloc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM