【问题标题】:flutter bloc BlocProvider.of(context) in the context of bottom sheet dialog在底部工作表对话框的上下文中颤动 bloc BlocProvider.of(context)
【发布时间】:2020-11-16 13:11:02
【问题描述】:

我在父小部件中提供了 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");
      },);
  }

}

【问题讨论】:

    标签: flutter bloc flutter-bloc


    【解决方案1】:

    如果你也想访问ReadingScreen() 中的同一个块,那么你需要用BlocProvider&lt;DevicePairBloc&gt; 包裹你的MaterialApp()。目前,您已将 DeviceList() 与该 bloc 提供程序包装在一起,因此它仅供它使用。

    【讨论】:

      猜你喜欢
      • 2021-12-20
      • 2020-02-16
      • 1970-01-01
      • 2021-02-08
      • 2021-12-25
      • 2019-08-14
      • 2019-06-15
      • 2020-04-24
      • 2021-12-02
      相关资源
      最近更新 更多