David Prinz
David Prinz

Reputation: 1

BlocProvider not found

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
        )

Upvotes: -1

Views: 42

Answers (1)

Dhruvin Vainsh
Dhruvin Vainsh

Reputation: 181

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.

Upvotes: 0

Related Questions