![](/img/trans.png)
[英]Flutter BLoC BlocProvider.of() called with a context that does not contain a Bloc of type
[英]flutter bloc BlocProvider.of(context) in the context of bottom sheet dialog
我在父小部件中提供了 bloc 并尝试在底部工作表对话框中访问该 bloc 实例。 在底部工作表对话框中,我有无法访问 bloc 实例的 bloc 构建器。
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider<DevicePairBloc>(
create: (context) => DevicePairBloc(),
child: DeviceList(),
);
}
}
class DeviceList extends StatefulWidget {
@override
State<StatefulWidget> createState() => _DeviceListState();
}
class _DeviceListState extends State<DeviceList> {
void showData(BuildContext context) {
showModalBottomSheet(
context: context, builder: (context) => ReadingScreen());
}
@override
Widget build(BuildContext context) {
return ListView(
children: <Widget>[
RaisedButton(onPressed: () => showData(context),),
],
);
}
}
class ReadingScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<DevicePairBloc, DevicePairState>(
builder: (context, state) {
return Text("20 mg");
},);
}
}
如果你也想在ReadingScreen()
中访问同一个块,那么你需要用BlocProvider<DevicePairBloc>
包装你的MaterialApp()
。 目前,您已经使用该 bloc 提供程序包装了DeviceList()
,因此它仅供它使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.