我在尝试分发事件的小部件上方有BlocProvider小部件,但我仍然得到BlocProvider.of() called with a context that does not contain a Bloc of type RenderBloc.
这是我的构建方法返回的内容:
return BlocProvider<RenderBloc>(
builder: (BuildContext context) => RenderBloc(),
child: Column(
children: <Widget>[
FlatButton(
child: Text('Render'),
onPressed: () {
BlocProvider.of<RenderBloc>(context).add(RenderProjectEvent(project));
},
)
],
),
);
我也尝试过MultiBlocProvider
,得到了相同的结果。
答案 0 :(得分:1)
您需要创建另一个内部上下文(例如,使用 Builder )来访问 InheritedWidget (提供者):
return BlocProvider<RenderBloc>(
builder: (BuildContext context) => RenderBloc(),
child: Builder(
builder: (cxt) {
return Column(
children: <Widget>[
FlatButton(
child: Text('Render'),
onPressed: () {
BlocProvider.of<RenderBloc>(cxt).add(RenderProjectEvent(project));
},
)
],
),
),
}
);