Skip to content

fix PopupMenuButton unmounted exception when updating position #166412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sstasi95
Copy link
Contributor

@sstasi95 sstasi95 commented Apr 2, 2025

This PR fixes an exception thrown when trying to update an unmounted PopupMenuButton's position, which can happen when a layout change is triggered during PopupMenuButton's pop animation (see issue's attached video).
A workaround is to set popUpAnimationStyle: AnimationStyle.noAnimation.
This PR fixes it by returning the last known position if the button is unmounted.

Exception thrown: FlutterError (This widget has been unmounted, so the State no longer has a context (and should be considered defunct). Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.)

Code that causes the exception: final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);

Fixes #163477

Tested both on stable (3.29.2) and master (a0b1b32)

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Apr 2, 2025
await tester.tap(find.byKey(buttonKey));
await tester.pumpAndSettle();

// TODO(sstasi95): here we should trigger a layout change to test the fix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can try pumping an unrelated widget with the tester (I.e. a SizedBox), after the button is tapped, to trigger the unmounting of the popup menu.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried but it's not enough, we should also update the layout after the popup menu is unmounted but when is still visible (during the pop animation), as you can see in this screen recording. This way the _positionBuilder method will be called on an unmounted popup menu.

@Piinks
Copy link
Contributor

Piinks commented Jun 4, 2025

Hey @sstasi95 it looks like this never entered our review queue since this is a draft. Would you like to move forward with this change?

@sstasi95
Copy link
Contributor Author

sstasi95 commented Jun 5, 2025

Hi @Piinks, sure. I left it as a draft because I wasn't able to write a test for it, I'll mark it ready for review now

@sstasi95 sstasi95 marked this pull request as ready for review June 5, 2025 09:45
@dkwingsmt dkwingsmt self-requested a review June 11, 2025 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PopupMenuRoute crashes sometimes on drawFrame
3 participants