InheritedWidget's of method is giving null upon using it inside AlertDialog.

I am using InheritedWidgets for the first time and I think it has been implemented correctly apart from one place. A ShowDialog that is getting called upon pressing of FAB. The of method of the InheritedWidget fails and the assert method returns null.
Code is below where it is called.

class ParameterDialog extends StatefulWidget {
  const ParameterDialog({super.key});

  @override
  State<ParameterDialog> createState() => _ParameterDialogState();
}

class _ParameterDialogState extends State<ParameterDialog> {
  @override
  Widget build(BuildContext context) {
    final inheritedProvider = HomePageInheritedWidget.of(context);
    return FloatingActionButton(
      key: const ValueKey('HomePageFloatingActionButton'),
      onPressed: () {
        setState(() {
          inheritedProvider.objects.textFieldEnabled = false;
          inheritedProvider.objects.textEditingController.clear();
        });
        showDialog(
          context: context,
          builder: (BuildContext context) {
final inheritedProvider=HomePageInheritedWidget.of(context);//this of method calling is returning null and the application halts.
            return StreamBuilder(
                  stream: inheritedProvider.objects.counterBloc.counter,
                  initialData: 2,
                  builder: (
                      BuildContext counterContext,
                      AsyncSnapshot<int> counterSnapshot,
                      ) {
                    return Center(
                      child: Card(
                        key: const ValueKey('CardForParameters'),
                        elevation: 20,
                        shadowColor: Colors.yellow,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            WidgetFailingIW(
                                counterSnapshot: counterSnapshot,),

I suppose it’s because when you push a new route or a new dialog (which is also in a new route), you can only access context above MaterialApp. Therefore you either move inherited widget / provider above material app or you have to pass your stuff in a different way

1 Like