flutter - 使用不包含“BlocName”的上下文调用 BlocProvider.of()
问题描述
我在使用MultiBlocProvider并通过小部件传递 blocContext 时遇到问题。我在 ShopScreen 中有一个 MultiBlocProvider,在其中有一个过滤器按钮,它显示来自 ModalBottomSheet 的类别列表。像这样:
当我从列表中选择不同的类别时,我必须更新商店屏幕。所以,我用过滤器按钮包装了这个 BlocConsumer,我必须将上下文传递给 modalBottomSheet,所以我使用了 BlocProvider.value - 当我按下过滤器按钮时,它会显示这个错误 - :
我的多块提供商:
MultiBlocProvider(
providers: [
BlocProvider<ProductsBloc>(create: (_) => productBloc),
BlocProvider<CategoryBloc>(create: (_) => CategoryBloc()),
BlocProvider<BannerBloc>(
create: (_) => BannerBloc()..add(ShowBannerEvent())),
],
过滤器按钮
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;
},
),
有什么遗漏吗?
解决方案
如果你想访问一个块,你应该使用context.read<CategoryBloc>()
.
例如:
context.read<CategoryBloc>().add(ShowCategoryEvent());
推荐阅读
- javascript - 无法在 LocalStorage 中保留单选按钮值
- macos - 有没有人成功地将 MAC 连接到 Azure P2S 网络网关?没有任何文档对我有用
- vue.js - 创建具有多个属性对象的 v-simple-table 表
- gis - 在 GeoDataFrame 中有超过 1 个几何列是不好的做法吗?
- powershell - Powershell变量替换不能从命令行工作
- swiftui - SwiftUI:根据属性值设置视图可见性?
- javascript - 打开具体化模态时内容变成白色
- docker - AzerothCore Docker 构建 cmake 错误:没有创建目标的规则
- tfs - TFS 2017 TF31002
- docker - 私有注册表自动下载