BlocProvider not found

28 Views Asked by At

For some reason when I was trying to show an alert dialog which needs a cubit created previously the cubit is not found.

As mentioned before when i am trying to provide a cubit with BlocProvider.value(value: context.read()), child: ...; to an alertDialog it wont find it...

SlidableAction(
          backgroundColor: block.sperrgrund != null ? Colors.orange : Colors.green,
          icon: block.sperrgrund != null ? Icons.lock : Icons.lock_open,
          onPressed: block.sperrgrund == null ? (context) {
            showDialog(
              context: context,
              builder: (context) {
                return BlocProvider.value(
                  value: context.read<DropdownCubit<Sperrgrund>>(),
                  child: SperrenPopup(
                    block: block as ZinkScan,
                    cubit: this.cubit!,
                  ),
                );
              }
              );
            } : null
        )
1

There are 1 best solutions below

0
Dhruvin Vainsh On

Try this instead:

SlidableAction(
          backgroundColor: block.sperrgrund != null ? Colors.orange : Colors.green,
          icon: block.sperrgrund != null ? Icons.lock : Icons.lock_open,
          onPressed: block.sperrgrund == null ? (context) {
            showDialog(
              context: context,
              builder: (_) {
                return BlocProvider.value(
                  value: context.read<DropdownCubit<Sperrgrund>>(),
                  child: SperrenPopup(
                    block: block as ZinkScan,
                    cubit: this.cubit!,
                  ),
                );
              }
              );
            } : null
        )

So, instead of the context provided by builder it will be using the context of its parent to scope the blockProvider and wrap SlidableAction with Builder if you have defined the BlockProvider inside the same build method from where you are trying to show the dialog.