Skip to content

PopupMenuRoute crashes sometimes on drawFrame #163477

Closed

Description

@bharathmg12

Steps to reproduce

  1. Add PopUpMenuButton and show it
  2. While the menu is being displayed, trigger drawFrame (such that PopupMenuRoute gets rebuilt)
  3. _positionBuilder inside PopupMenuRoute crashes due to the stale context

This crash only happens with this commit efa7d52. Seems like positionBuilder now calculates position dynamically, but it holds to the invalidated context sometimes.

Crash trace

_TypeError: Null check operator used on a null value

at Navigator.of	(navigator.dart:2894)

at PopupMenuButtonState._positionBuilder	(popup_menu.dart:1522)

at _PopupMenuRoute.buildPage.<anonymous closure>	(popup_menu.dart:972)

at _LayoutBuilderElement._rebuildWithConstraints.updateChildCallback	(layout_builder.dart:201)
at BuildOwner.buildScope	(framework.dart:3056)
at _LayoutBuilderElement._rebuildWithConstraints	(layout_builder.dart:240)
at _LayoutBuilderElement._rebuildWithConstraints	(layout_builder.dart:196)
at RenderObject.invokeLayoutCallback.<anonymous closure>	(object.dart:2827)
at PipelineOwner._enableMutationsToDirtySubtrees	(object.dart:1161)
at RenderObject.invokeLayoutCallback	(object.dart:2826)
at RenderConstrainedLayoutBuilder.rebuildIfNecessary	(layout_builder.dart:293)
at _RenderLayoutBuilder.performLayout	(layout_builder.dart:390)
at RenderObject.layout	(object.dart:2715)
at RenderProxyBoxMixin.performLayout	(proxy_box.dart:115)
at RenderObject.layout	(object.dart:2715)
at RenderProxyBoxMixin.performLayout	(proxy_box.dart:115)
at RenderObject.layout	(object.dart:2715)
at RenderProxyBoxMixin.performLayout	(proxy_box.dart:115)
at RenderObject.layout	(object.dart:2715)
at RenderProxyBoxMixin.performLayout	(proxy_box.dart:115)
at RenderObject.layout	(object.dart:2715)
at RenderProxyBoxMixin.performLayout	(proxy_box.dart:115)
at RenderOffstage.performLayout	(proxy_box.dart:3750)
at RenderObject.layout	(object.dart:2715)
at RenderProxyBoxMixin.performLayout	(proxy_box.dart:115)
at RenderObject.layout	(object.dart:2715)
at _RenderTheaterMixin.layoutChild	(overlay.dart:1076)
at _RenderTheater.performLayout	(overlay.dart:1422)
at RenderObject._layoutWithoutResize	(object.dart:2548)
at PipelineOwner.flushLayout	(object.dart:1112)
at PipelineOwner.flushLayout	(object.dart:1125)
at RendererBinding.drawFrame	(binding.dart:616)
at WidgetsBinding.drawFrame	(binding.dart:1231)
at RendererBinding._handlePersistentFrameCallback	(binding.dart:482)
at RendererBinding._handlePersistentFrameCallback	(binding.dart:481)
at SchedulerBinding._invokeFrameCallback	(binding.dart:1442)
at SchedulerBinding.handleDrawFrame	(binding.dart:1355)
at SchedulerBinding._handleDrawFrame	(binding.dart:1208)
at SchedulerBinding._handleDrawFrame	(binding.dart:1190)
at _invoke	(hooks.dart:316)
at PlatformDispatcher._drawFrame	(platform_dispatcher.dart:428)
at _drawFrame	(hooks.dart:288)
at _drawFrame	(hooks.dart:286)

Expected results

Popup menu stays displayed

Actual results

App Crashes

Code sample

<>

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
✓] Flutter (Channel google3, on Debian GNU/Linux rodete 6.10.11-1rodete2-amd64, locale en_US.UTF-8) [93ms]
    • Framework revision 5944d992ac (11 days ago), 2025-02-06T00:00:00.000
    • Engine revision 5944d992ac
    • Dart version 548dcc7a54

[✓] Android toolchain - develop for Android devices (Android SDK version Stable) [93ms]
    • Android SDK at google3
    • Platform Stable, build-tools Stable
    • Java binary at: /opt/android-studio-with-blaze-stable/jbr/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.5+-12932927-b750.29)

[!] Flutter IDE Support (No supported IDEs installed) [0ms]
    • IntelliJ - https://www.jetbrains.com/idea/
    • Android Studio - https://developer.android.com/studio/
    • VS Code - https://code.visualstudio.com/

[✓] Connected device (1 available) [22ms]
    • Linux (desktop) • linux • linux-x64 • Debian GNU/Linux rodete 6.10.11-1rodete2-amd64

Activity

  1. yuwen-yan commented on Feb 18, 2025

    @yuwen-yan
    Contributor

    @YeungKC it seems you touched the related code recently in efa7d52, can you help take a look regarding the potential root cause or any additional null check we can perform in the code to be on the safer side.

  2. YeungKC commented on Feb 18, 2025

    @YeungKC
    Member

    I've already made an attempt, but I couldn't reproduce the success. Could you provide me with a minimal reproducible example?

  3. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Feb 18, 2025
  4. matanlurey commented on Feb 18, 2025

    @matanlurey
    Contributor

    @ChristianEdwardPadilla @bharathmg12 This issue needs a minimal reproducible sample as mentioned in the bug template:

    Image

  5. yuwen-yan commented on Feb 19, 2025

    @yuwen-yan
    Contributor

    @YeungKC thanks for following on it! is it possible to add some null checks in the code (according to the crash stack trace in #163477 (comment) ) to make the code more robust to give it a try?

    @matanlurey I understand that it's important to have a mini-reproducible sample for the investigation, not sure whether @bharathmg12 can help on it, I personally tried the following code, unfortunately, it can't reproduce it. let me know anything else we can do to help the investigation.

    import 'package:flutter/material.dart';
    import 'dart:async'; // Import the async library for Timer
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      bool _rebuildParent = false;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('PopupMenu Crash Repro'),
          ),
          body: Column(
            children: [
              if (_rebuildParent)
                const Text('rebuilt widget'),
              if (!_rebuildParent)
                PopupMenuButton(
                  itemBuilder: (context) => [
                    PopupMenuItem(
                      child: Text('Item 3'),
                    ),
                    PopupMenuItem(
                      child: Text('Item 4'),
                    ),
                  ],
                ),
              ElevatedButton(
                onPressed: () {
                  Timer(Duration(seconds: 5), () {
                    setState(() {
                      _rebuildParent = !_rebuildParent;
                    });
                  });
                },
                child: Text('Toggle Parent Rebuild'),
              ),
            ],
          ),
        );
      }
    }
    
  6. YeungKC commented on Feb 19, 2025

    @YeungKC
    Member

    I think we could add some null checks, but the problem is that I can't write tests...

    cc @dkwingsmt

  7. dkwingsmt commented on Feb 19, 2025

    @dkwingsmt
    Contributor

    @YeungKC Do you think it's possible to write test so that you just pump the test and do the gesture, and if it doesn't crash the test passes? (i.e. It doesn't have to have an expect to be a test.)

  8. YeungKC commented on Feb 19, 2025

    @YeungKC
    Member

    @dkwingsmt Yes, I also hope to write a test that can crash 100% of the time to help me fix the bug, However, it seems that none of us can reproduce it at the moment.

    Add null check should most likely fix the crash issue, but it doesn't sure that the position will be the expected position.

  9. dkwingsmt commented on Feb 19, 2025

    @dkwingsmt
    Contributor

    Yeah we need a reliable reproduction to start fixing this issue.

    @bharathmg12 Can you try to find more reliable reproductions of this error?

  10. github-actions commented on Mar 11, 2025

    @github-actions
  11. yuwen-yan commented on Mar 13, 2025

    @yuwen-yan
    Contributor

    @dkwingsmt is it possible to reopen this issue (I have no permission)? this crash is still happening in our app:/

    @bharathmg12 gentle ping, let us know whether you can reproduce it, thanks in advance!

    @YeungKC If we don't feel confident to fix the crash directly, is it possible perform additional check and throw a different exception to help us understand the crash first?

  12. 24 remaining items

  13. math1man commented on Jul 30, 2025

    @math1man
    Contributor

    My app is also facing this issue (internal b/434889540, client huggsy). I'll see if the fix addresses the crashes in our next release.

  14. math1man commented on Aug 11, 2025

    @math1man
    Contributor

    We haven't seen any reports from our latest app release that includes #166412.

  15. math1man commented on Aug 11, 2025

    @math1man
    Contributor

    Worth noting that that release does not include #171970

  16. yuwen-yan commented on Aug 15, 2025

    @yuwen-yan
    Contributor

    @huycozy thank you! it's working in our app.

  17. huycozy commented on Aug 18, 2025

    @huycozy
    Member

    Thank you all for confirming and re-testing this!

    @dkwingsmt: Can we safely close this issue since @yuwen-yan confirmed above, or should we wait for @bharathmg12 to confirm as well?

  18. dmitry-fbm commented on Aug 18, 2025

    @dmitry-fbm

    Can we safely close this issue

    @huycozy was it released in 3.35.1?

  19. huycozy commented on Aug 18, 2025

    @huycozy
    Member

    @huycozy was it released in 3.35.1?

    @dmitry-fbm Not yet as I can see: db38e71. However, it's on beta 3.36.0-0.1.pre already.

  20. dkwingsmt commented on Aug 19, 2025

    @dkwingsmt
    Contributor

    Since a lot of people have confirmed the fix, I'm closing this issue. Feel free to reopen this issue or open a new one if found otherwise.

    Thank you everyone for either working on it, reporting the bug, trying to reproduce it, and providing inspirations!

  21. added
    r: fixedIssue is closed as already fixed in a newer version
    on Aug 20, 2025
  22. github-actions commented on Sep 3, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  23. locked as resolved and limited conversation to collaborators on Sep 3, 2025
  24. added a commit that references this issue on Nov 21, 2025
    97d0915
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High-priority issues at the top of the work listc: crashStack traces logged to the consolecustomer: googleVarious Google teamscustomer: money (g3)needs repro infoAutomated crash report whose cause isn't yet knownr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions