0

I have provided bloc in parent widget and trying to access that bloc instance in bottom sheet dialog. In bottom sheet dialog i have bloc builder which can not access the bloc instance.

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");
      },);
  }

}
c__c
  • 1,574
  • 1
  • 19
  • 39

1 Answers1

0

If you want to access the same bloc in ReadingScreen() too, then you'll need to wrap your MaterialApp() with BlocProvider<DevicePairBloc>. At the moment you have wrapped the DeviceList() with that bloc provider, so it is only available to it.

Jigar Patel
  • 4,953
  • 1
  • 12
  • 20