Dima
Dima

Reputation: 1249

PopupMenuButton. Looking up a deactivated widget's ancestor is unsafe

I have a PopupMenuButton:

              PopupMenuButton<TemplateMenuItem>(

                iconColor: blueButtonColor,
                // initialValue: ExerciseWorkoutMenuItem.note,
                onSelected: (TemplateMenuItem item) async {
                  switch (item) {
                    case TemplateMenuItem.delete:
                      {
      

                        
                      }
                  }
                },

                itemBuilder: (BuildContext context) =>
                    <PopupMenuEntry<TemplateMenuItem>>[
                  PopupMenuItem<TemplateMenuItem>(
                    value: TemplateMenuItem.delete,
                    child: Text("Delete".tr()),
                  ),
                ],
              ),   

It is inside in DragAndDropLists. DragAndDropLists is in ValueListenableBuilder. When value changes, DragAndDropLists refreshes. Everything w orks fine, but after adding a new item, the first time I click on PopupMenuButton I got error:

Looking up a deactivated widget's ancestor is unsafe.

To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

The second click is ok.I searched for a solution to this error but none helped me.

Upvotes: 0

Views: 37

Answers (1)

Bekir Taşkın
Bekir Taşkın

Reputation: 11

I have faced the same issue, captureInheritedThemes property, that is mentined as solution to this problem in other questions, is not available for Flutter 3.29.0.

You can use showMenu instead of PopupMenuButton. The negative side of showMenu is positioning should be handled manually.

The other option that works for me is defining GlobalKey in global space (out of the class). With this key, popupMenuButton works as expected.

Upvotes: 0

Related Questions