BlocProvider.of() 使用不包含 MainBloc 类型的 Bloc 的上下文调用
BlocProvider.of() called with a context that does not contain a Bloc of type MainBloc
我有一个位于主路由内的 MainBloc,这条路由有一个带有多个子路由的底部应用栏,我希望在所有五个子路由上使用相同的 BLoC 运行,这样当一个他们中的一个改变了方块的状态,其他人会看到效果。
我尝试了 SO 问题,但它与我正在寻找的问题相去甚远,我也尝试按照错误建议我去做,但没有成功,这是我得到的消息:
This can happen if:
1. The context you used comes from a widget above the BlocProvider.
2. You used MultiBlocProvider and didn't explicity provide the BlocProvider types.
Good: BlocProvider<MainBloc>(builder: (context) => MainBloc())
Bad: BlocProvider(builder: (context) => MainBloc()).
主要路线:
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<MainBloc>(
builder: (BuildContext context) => MainBloc(),
),
BlocProvider<OtherBloc>(
builder: (BuildContext context) => OtherBloc(),
),
],
child: /..., //here I have the bottom app bar with 5 buttons to navigate between sub-routes
);
子路线之一:
@override
Widget build(BuildContext context) {
final MainBloc bloc = BlocProvider.of<MainBloc>(context);
return /...; //here I have the context of this sub-route.
}
根据我从教程和文章中看到的内容,这段代码应该可以工作,但我似乎找不到原因。
因为这个 child 有底部应用栏:
child: /..., //here I have the bottom app bar
然后我假设 MultiBlocProvider(..)
没有包装使用这个 Bloc 的整个应用程序部分,我在这里的建议是用 "MultiBlocProvider".
包装 "MaterialApp"
return MultiBlocProvider(
providers: [..],
child: MaterialApp(..) // Set MaterialApp as the child of the MultiBlocProvider
//..
)
问题是您无法跨路由访问 InheritedWidgets,除非您在 MaterialApp 之上提供 InheritedWidget。我建议将您的新路线包装在 BlocProvider.value 中,以将现有集团提供给新路线,例如:
Navigator.of(context).push(
MaterialPageRoute<MyPage>(
builder: (_) {
return BlocProvider.value(
value: BlocProvider.of<MyBloc>(context),
child: MyPage(),
);
},
),
);
您可以在 bloc documentation
中找到有关此内容的更多详细信息
我有一个位于主路由内的 MainBloc,这条路由有一个带有多个子路由的底部应用栏,我希望在所有五个子路由上使用相同的 BLoC 运行,这样当一个他们中的一个改变了方块的状态,其他人会看到效果。
我尝试了
This can happen if:
1. The context you used comes from a widget above the BlocProvider.
2. You used MultiBlocProvider and didn't explicity provide the BlocProvider types.
Good: BlocProvider<MainBloc>(builder: (context) => MainBloc())
Bad: BlocProvider(builder: (context) => MainBloc()).
主要路线:
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<MainBloc>(
builder: (BuildContext context) => MainBloc(),
),
BlocProvider<OtherBloc>(
builder: (BuildContext context) => OtherBloc(),
),
],
child: /..., //here I have the bottom app bar with 5 buttons to navigate between sub-routes
);
子路线之一:
@override
Widget build(BuildContext context) {
final MainBloc bloc = BlocProvider.of<MainBloc>(context);
return /...; //here I have the context of this sub-route.
}
根据我从教程和文章中看到的内容,这段代码应该可以工作,但我似乎找不到原因。
因为这个 child 有底部应用栏:
child: /..., //here I have the bottom app bar
然后我假设 MultiBlocProvider(..)
没有包装使用这个 Bloc 的整个应用程序部分,我在这里的建议是用 "MultiBlocProvider".
return MultiBlocProvider(
providers: [..],
child: MaterialApp(..) // Set MaterialApp as the child of the MultiBlocProvider
//..
)
问题是您无法跨路由访问 InheritedWidgets,除非您在 MaterialApp 之上提供 InheritedWidget。我建议将您的新路线包装在 BlocProvider.value 中,以将现有集团提供给新路线,例如:
Navigator.of(context).push(
MaterialPageRoute<MyPage>(
builder: (_) {
return BlocProvider.value(
value: BlocProvider.of<MyBloc>(context),
child: MyPage(),
);
},
),
);
您可以在 bloc documentation
中找到有关此内容的更多详细信息