[英]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:像这样:
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.