Skip to content

[go_router] Open PopUpMenuButton causes crash when navigating to new base route using pageBuilder #167180

Closed

Description

@tylerbarnesTrexis

What package does this bug report belong to?

go_router

What target platforms are you seeing this bug on?

iOS

Have you already upgraded your packages?

Yes

Dependency versions

pubspec.lock
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
  async:
    dependency: transitive
    description:
      name: async
      sha256: d2872f9c19731c2e5f10444b14686eb7cc85c76274bd6c16e1816bff9a3bab63
      url: "https://pub.dev"
    source: hosted
    version: "2.12.0"
  bloc:
    dependency: transitive
    description:
      name: bloc
      sha256: "658a5ae59edcf1e58aac98b000a71c762ad8f46f1394c34a52050cafb3e11a80"
      url: "https://pub.dev"
    source: hosted
    version: "8.1.1"
  boolean_selector:
    dependency: transitive
    description:
      name: boolean_selector
      sha256: "8aab1771e1243a5063b8b0ff68042d67334e3feab9e95b9490f9a6ebf73b42ea"
      url: "https://pub.dev"
    source: hosted
    version: "2.1.2"
  characters:
    dependency: transitive
    description:
      name: characters
      sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803
      url: "https://pub.dev"
    source: hosted
    version: "1.4.0"
  clock:
    dependency: transitive
    description:
      name: clock
      sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b
      url: "https://pub.dev"
    source: hosted
    version: "1.1.2"
  collection:
    dependency: transitive
    description:
      name: collection
      sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76"
      url: "https://pub.dev"
    source: hosted
    version: "1.19.1"
  cupertino_icons:
    dependency: "direct main"
    description:
      name: cupertino_icons
      sha256: e35129dc44c9118cee2a5603506d823bab99c68393879edb440e0090d07586be
      url: "https://pub.dev"
    source: hosted
    version: "1.0.5"
  equatable:
    dependency: "direct main"
    description:
      name: equatable
      sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
      url: "https://pub.dev"
    source: hosted
    version: "2.0.5"
  fake_async:
    dependency: transitive
    description:
      name: fake_async
      sha256: "6a95e56b2449df2273fd8c45a662d6947ce1ebb7aafe80e550a3f68297f3cacc"
      url: "https://pub.dev"
    source: hosted
    version: "1.3.2"
  flutter:
    dependency: "direct main"
    description: flutter
    source: sdk
    version: "0.0.0"
  flutter_bloc:
    dependency: "direct main"
    description:
      name: flutter_bloc
      sha256: "434951eea948dbe87f737b674281465f610b8259c16c097b8163ce138749a775"
      url: "https://pub.dev"
    source: hosted
    version: "8.1.2"
  flutter_lints:
    dependency: "direct dev"
    description:
      name: flutter_lints
      sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c
      url: "https://pub.dev"
    source: hosted
    version: "2.0.1"
  flutter_test:
    dependency: "direct dev"
    description: flutter
    source: sdk
    version: "0.0.0"
  flutter_web_plugins:
    dependency: transitive
    description: flutter
    source: sdk
    version: "0.0.0"
  go_router:
    dependency: "direct main"
    description:
      name: go_router
      sha256: b4bb06205ec607278b6fc23db238278417bca84a3905779cc68d1eb7afae37e2
      url: "https://pub.dev"
    source: hosted
    version: "6.2.0"
  leak_tracker:
    dependency: transitive
    description:
      name: leak_tracker
      sha256: c35baad643ba394b40aac41080300150a4f08fd0fd6a10378f8f7c6bc161acec
      url: "https://pub.dev"
    source: hosted
    version: "10.0.8"
  leak_tracker_flutter_testing:
    dependency: transitive
    description:
      name: leak_tracker_flutter_testing
      sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573
      url: "https://pub.dev"
    source: hosted
    version: "3.0.9"
  leak_tracker_testing:
    dependency: transitive
    description:
      name: leak_tracker_testing
      sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
      url: "https://pub.dev"
    source: hosted
    version: "3.0.1"
  lints:
    dependency: transitive
    description:
      name: lints
      sha256: "5e4a9cd06d447758280a8ac2405101e0e2094d2a1dbdd3756aec3fe7775ba593"
      url: "https://pub.dev"
    source: hosted
    version: "2.0.1"
  logging:
    dependency: transitive
    description:
      name: logging
      sha256: "04094f2eb032cbb06c6f6e8d3607edcfcb0455e2bb6cbc010cb01171dcb64e6d"
      url: "https://pub.dev"
    source: hosted
    version: "1.1.1"
  matcher:
    dependency: transitive
    description:
      name: matcher
      sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2
      url: "https://pub.dev"
    source: hosted
    version: "0.12.17"
  material_color_utilities:
    dependency: transitive
    description:
      name: material_color_utilities
      sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
      url: "https://pub.dev"
    source: hosted
    version: "0.11.1"
  meta:
    dependency: transitive
    description:
      name: meta
      sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
      url: "https://pub.dev"
    source: hosted
    version: "1.16.0"
  nested:
    dependency: transitive
    description:
      name: nested
      sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
      url: "https://pub.dev"
    source: hosted
    version: "1.0.0"
  path:
    dependency: transitive
    description:
      name: path
      sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
      url: "https://pub.dev"
    source: hosted
    version: "1.9.1"
  provider:
    dependency: transitive
    description:
      name: provider
      sha256: cdbe7530b12ecd9eb455bdaa2fcb8d4dad22e80b8afb4798b41479d5ce26847f
      url: "https://pub.dev"
    source: hosted
    version: "6.0.5"
  sky_engine:
    dependency: transitive
    description: flutter
    source: sdk
    version: "0.0.0"
  source_span:
    dependency: transitive
    description:
      name: source_span
      sha256: "254ee5351d6cb365c859e20ee823c3bb479bf4a293c22d17a9f1bf144ce86f7c"
      url: "https://pub.dev"
    source: hosted
    version: "1.10.1"
  stack_trace:
    dependency: transitive
    description:
      name: stack_trace
      sha256: "8b27215b45d22309b5cddda1aa2b19bdfec9df0e765f2de506401c071d38d1b1"
      url: "https://pub.dev"
    source: hosted
    version: "1.12.1"
  stream_channel:
    dependency: transitive
    description:
      name: stream_channel
      sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d"
      url: "https://pub.dev"
    source: hosted
    version: "2.1.4"
  string_scanner:
    dependency: transitive
    description:
      name: string_scanner
      sha256: "921cd31725b72fe181906c6a94d987c78e3b98c2e205b397ea399d4054872b43"
      url: "https://pub.dev"
    source: hosted
    version: "1.4.1"
  term_glyph:
    dependency: transitive
    description:
      name: term_glyph
      sha256: "7f554798625ea768a7518313e58f83891c7f5024f88e46e7182a4558850a4b8e"
      url: "https://pub.dev"
    source: hosted
    version: "1.2.2"
  test_api:
    dependency: transitive
    description:
      name: test_api
      sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd
      url: "https://pub.dev"
    source: hosted
    version: "0.7.4"
  vector_math:
    dependency: transitive
    description:
      name: vector_math
      sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
      url: "https://pub.dev"
    source: hosted
    version: "2.1.4"
  vm_service:
    dependency: transitive
    description:
      name: vm_service
      sha256: "0968250880a6c5fe7edc067ed0a13d4bae1577fe2771dcf3010d52c4a9d3ca14"
      url: "https://pub.dev"
    source: hosted
    version: "14.3.1"
sdks:
  dart: ">=3.7.0-0 <4.0.0"
  flutter: ">=3.18.0-18.0.pre.54"

Steps to reproduce

Used https://github.com/MohanedZekry/flutter-go-router-example to reproduce:

  1. Replace home_screen.dart:
import 'package:flutter/material.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: PopupMenuButton<String>(
        icon: const Icon(Icons.menu, color: Colors.white),
        elevation: 16.0,
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(4.0)),
        ),
        position: PopupMenuPosition.under,
        offset: const Offset(0, 16),
        itemBuilder: (BuildContext context) {
          return [
            const PopupMenuItem<String>(
              value: 'item1',
              child: Text('Item 1'),
            ),
          ];
        },
      ),
    );
  }
}
  1. Open PopUpMenu
  2. Navigate to any BottomMenuItem (other than HOME)

Expected results

Pop up should dismiss without error
Go_Router navigation should own the WidgetTree and navigate

Actual results

App shows red screen with following error, Go_Router navigation occurs beneath:

════════ Exception caught by widgets library ═══════════════════════════════════ The following assertion was thrown building LayoutBuilder: 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 sample

Code sample

See Above Repro Steps

Screenshots or Videos

No response

Logs

No response

Flutter Doctor output

Doctor output
% flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.29.2, on macOS 15.4 24E248 darwin-x64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] VS Code (version 1.99.2)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

Activity

  1. maheshj01 commented on Apr 15, 2025

    @maheshj01
    Member

    @tylerbarnesTrexis Thanks for filing the issue. I am able to reproduce the issue on stable and the master channel the exception doest not occur if you use a normal builder in place of pageBuilder

    minimal code sample
    import 'package:flutter/material.dart';
    import 'package:go_router/go_router.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      State<MyApp> createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      final List<BottomNavigationBarItem> tabs = [
        const BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
        const BottomNavigationBarItem(
          icon: Icon(Icons.screen_share),
          label: 'Screen A',
        ),
        const BottomNavigationBarItem(
          icon: Icon(Icons.screen_rotation),
          label: 'Screen B',
        ),
      ];
      final GoRouter _router = GoRouter(
        routes: [
          ShellRoute(
            builder: (context, state, child) {
              return Scaffold(
                backgroundColor: Colors.red,
                appBar: AppBar(),
                body: child,
                bottomNavigationBar: BottomNavigationBar(
                  items: [
                    const BottomNavigationBarItem(
                      icon: Icon(Icons.home),
                      label: 'Home',
                    ),
                    const BottomNavigationBarItem(
                      icon: Icon(Icons.screen_share),
                      label: 'Screen A',
                    ),
                    const BottomNavigationBarItem(
                      icon: Icon(Icons.screen_rotation),
                      label: 'Screen B',
                    ),
                  ],
                  onTap: (index) {
                    switch (index) {
                      case 0:
                        context.go('/');
                        break;
                      case 1:
                        context.go('/screenA');
                        break;
                      case 2:
                        context.go('/screenB');
                        break;
                    }
                  },
                ),
              );
            },
            routes: [
              GoRoute(
                path: '/',
                name: 'home',
                // builder: (context, state) => HomeScreen(),
                pageBuilder:
                    (context, state) => NoTransitionPage(child: HomeScreen()),
              ),
              GoRoute(
                path: '/screenA',
                name: 'screenA',
                // builder: (context, state) => ScreenA(),
                pageBuilder: (context, state) => NoTransitionPage(child: ScreenA()),
              ),
              GoRoute(
                path: '/screenB',
                name: 'screenB',
                // builder: (context, state) => ScreenB(),
                pageBuilder: (context, state) => NoTransitionPage(child: ScreenB()),
              ),
            ],
          ),
        ],
      );
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp.router(routerConfig: _router);
      }
    }
    
    class HomeScreen extends StatelessWidget {
      const HomeScreen({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Center(
          child: PopupMenuButton<String>(
            icon: const Icon(Icons.menu, color: Colors.white),
            elevation: 16.0,
            shape: const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(4.0)),
            ),
            position: PopupMenuPosition.under,
            offset: const Offset(0, 16),
            itemBuilder: (BuildContext context) {
              return [
                const PopupMenuItem<String>(value: 'item1', child: Text('Item 1')),
              ];
            },
          ),
        );
      }
    }
    
    class ScreenA extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Screen A')),
          body: Center(child: Text('Welcome to Screen A')),
        );
      }
    }
    
    class ScreenB extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Screen B')),
          body: Center(child: Text('Welcome to Screen B')),
        );
      }
    }
    
    logs
    mahesh@MacBook-Pro-10 flutter-go-router-example % flutter run -d chrome
    Launching lib/main.dart on Chrome in debug mode...
    Warning: In index.html:37: Local variable for "serviceWorkerVersion" is deprecated. Use
    "{{flutter_service_worker_version}}" template token instead. See
    https://docs.flutter.dev/platform-integration/web/initialization for more details.
    Warning: In index.html:46: "FlutterLoader.loadEntrypoint" is deprecated. Use "FlutterLoader.load" instead.
    See https://docs.flutter.dev/platform-integration/web/initialization for more details.
    Waiting for connection from debug service on Chrome...             12.0s
    This app is linked to the debug service: ws://127.0.0.1:60806/I2fNNMp9A9g=/ws
    Debug service listening on ws://127.0.0.1:60806/I2fNNMp9A9g=/ws
    
    🔥  To hot restart changes while running, press "r" or "R".
    For a more detailed help message, press "h". To quit, press "q".
    
    A Dart VM Service on Chrome is available at: http://127.0.0.1:60806/I2fNNMp9A9g=
    The Flutter DevTools debugger and profiler on Chrome is available at:
    http://127.0.0.1:9102?uri=http://127.0.0.1:60806/I2fNNMp9A9g=
    
    Warning: In index.html:37: Local variable for "serviceWorkerVersion" is deprecated. Use
    "{{flutter_service_worker_version}}" template token instead. See
    https://docs.flutter.dev/platform-integration/web/initialization for more details.
    Warning: In index.html:46: "FlutterLoader.loadEntrypoint" is deprecated. Use "FlutterLoader.load" instead.
    See https://docs.flutter.dev/platform-integration/web/initialization for more details.
    Performing hot restart...                                        1,619ms
    Restarted application in 1,620ms.
    ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
    The following assertion was thrown building LayoutBuilder:
    Looking up a deactivated widget's ancestor is unsafe.
    At this point the state of the widget's element tree is no longer stable.
    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 relevant error-causing widget was:
      Navigator-[LabeledGlobalKey<NavigatorState>#2daf6]
      Navigator:file:///Users/mahesh/.pub-cache/hosted/pub.dev/go_router-6.2.0/lib/src/builder.dart:248:12
    
    When the exception was thrown, this was the stack:
    dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 307:3     throw_
    packages/flutter/src/widgets/framework.dart 4963:9                              <fn>
    packages/flutter/src/widgets/framework.dart 4976:14
    [_debugCheckStateIsActiveForAncestorLookup]
    packages/flutter/src/widgets/framework.dart 4997:12
    dependOnInheritedWidgetOfExactType
    packages/flutter/src/material/popup_menu_theme.dart 279:16                      of
    packages/flutter/src/material/popup_menu.dart 1519:62                           [_positionBuilder]
    packages/flutter/src/material/popup_menu.dart 972:32                            <fn>
    packages/flutter/src/widgets/layout_builder.dart 201:70                         updateChildCallback
    packages/flutter/src/widgets/framework.dart 3056:11                             buildScope
    packages/flutter/src/widgets/layout_builder.dart 240:5                          [_rebuildWithConstraints]
    packages/flutter/src/rendering/object.dart 2827:9                               <fn>
    packages/flutter/src/rendering/object.dart 1161:7
    [_enableMutationsToDirtySubtrees]
    packages/flutter/src/rendering/object.dart 2826:7                               invokeLayoutCallback
    packages/flutter/src/widgets/layout_builder.dart 293:5                          rebuildIfNecessary
    packages/flutter/src/widgets/layout_builder.dart 390:5                          performLayout
    packages/flutter/src/rendering/object.dart 2548:7                               [_layoutWithoutResize]
    packages/flutter/src/rendering/object.dart 1112:17                              flushLayout
    packages/flutter/src/rendering/object.dart 1125:14                              flushLayout
    packages/flutter/src/rendering/binding.dart 616:5                               drawFrame
    packages/flutter/src/widgets/binding.dart 1231:13                               drawFrame
    packages/flutter/src/rendering/binding.dart 482:5
    [_handlePersistentFrameCallback]
    packages/flutter/src/scheduler/binding.dart 1442:7                              [_invokeFrameCallback]
    packages/flutter/src/scheduler/binding.dart 1355:9                              handleDrawFrame
    packages/flutter/src/scheduler/binding.dart 1208:5                              [_handleDrawFrame]
    lib/_engine/engine/platform_dispatcher.dart 1347:5                              invoke
    lib/_engine/engine/platform_dispatcher.dart 301:5                               invokeOnDrawFrame
    lib/_engine/engine/initialization.dart 190:36                                   <fn>
    dart-sdk/lib/_internal/js_dev_runtime/patch/js_allow_interop_patch.dart 224:27  _callDartFunctionFast1
    
    ════════════════════════════════════════════════════════════════════════════════════════════════════
    
    Warning: In index.html:37: Local variable for "serviceWorkerVersion" is deprecated. Use
    "{{flutter_service_worker_version}}" template token instead. See
    https://docs.flutter.dev/platform-integration/web/initialization for more details.
    Warning: In index.html:46: "FlutterLoader.loadEntrypoint" is deprecated. Use "FlutterLoader.load" instead.
    See https://docs.flutter.dev/platform-integration/web/initialization for more details.
    Performing hot restart...                                        1,245ms
    Restarted application in 1,245ms.
    
    Application finished.
    flutter doctor
    [!] Flutter (Channel stable, 3.29.2, on macOS 15.4 24E248 darwin-arm64, locale en-US) [2.6s]
        • Flutter version 3.29.2 on channel stable at /Users/mahesh/Development/flutter
        ! Warning: `flutter` on your path resolves to /Users/mahesh/Development/flutter_master/bin/flutter, which is not inside your
          current Flutter SDK checkout at /Users/mahesh/Development/flutter. Consider adding /Users/mahesh/Development/flutter/bin to the
          front of your path.
        ! Warning: `dart` on your path resolves to /Users/mahesh/Development/flutter_master/bin/dart, which is not inside your current
          Flutter SDK checkout at /Users/mahesh/Development/flutter. Consider adding /Users/mahesh/Development/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision c236373904 (5 weeks ago), 2025-03-13 16:17:06 -0400
        • Engine revision 18b71d647a
        • Dart version 3.7.2
        • DevTools version 2.42.3
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform
          update checks and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [4.2s]
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-35, build-tools 35.0.0
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/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)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.2) [1,605ms]
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 16C5032a
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [137ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [136ms]
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 21.0.5+-12932927-b750.29)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1) [133ms]
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.96.2) [18ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.102.0
    
    [✓] Connected device (3 available) [7.2s]
        • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 15.4 24E248 darwin-arm64
        • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin         • macOS 15.4 24E248 darwin-arm64
        • Chrome (web)                    • chrome                • web-javascript • Google Chrome 131.0.6778.265
    
    [✓] Network resources [299ms]
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category
    [✓] Flutter (Channel master, 3.32.0-1.0.pre.90, on macOS 15.4 24E248 darwin-arm64, locale en-US) [25.8s]
        • Flutter version 3.32.0-1.0.pre.90 on channel master at /Users/mahesh/Development/flutter_master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 29810cd6d1 (2 hours ago), 2025-04-16 00:07:50 +0530
        • Engine revision 29810cd6d1
        • Dart version 3.8.0 (build 3.8.0-265.0.dev)
        • DevTools version 2.45.0-dev.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [1,817ms]
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-35, build-tools 35.0.0
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/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)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.2) [1,140ms]
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 16C5032a
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [128ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [127ms]
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 21.0.5+-12932927-b750.29)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1) [125ms]
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.96.2) [18ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.102.0
    
    [✓] Connected device (2 available) [6.3s]
        • macOS (desktop) • macos  • darwin-arm64   • macOS 15.4 24E248 darwin-arm64
        • Chrome (web)    • chrome • web-javascript • Google Chrome 131.0.6778.265
    
    [✓] Network resources [333ms]
        • All expected network resources are available.
    
    • No issues found!
    
  2. added
    packageflutter/packages repository. See also p: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    on Apr 15, 2025
  3. changed the title [-][go_router] Open PopUpMenuButton causes crash when navigating to new base route[/-] [+][go_router] Open PopUpMenuButton causes crash when navigating to new base route using pageBuilder[/+] on Apr 15, 2025
  4. tylerbarnesTrexis commented on Apr 16, 2025

    @tylerbarnesTrexis
    Author

    Good workaround for now, I'll see if I can make that shift work in our codebase.

  5. krille-chan commented on May 29, 2025

    @krille-chan

    I replaced all pageBuilder with builder in my GoRouter and the error still occurs. This is unfortunately a deal breaker for me. Didn't happen in previous versions of GoRouter. Only other workaround is to set popUpAnimationStyle: AnimationStyle.noAnimation in all Popupmenu buttons. However this looks boring and is no option for me

  6. 1 remaining item

  7. keisei0214 commented on Jul 21, 2025

    @keisei0214

    It seems that by extending PopupMenuButton and overriding the popup behavior as shown below, errorcan be avoided.

    Code Sample
    import 'package:flutter/material.dart';
    
    class CustomPopupMenuButton<T> extends PopupMenuButton<T> {
      const CustomPopupMenuButton({
        super.key,
        required super.itemBuilder,
        super.initialValue,
        super.onOpened,
        super.onSelected,
        super.onCanceled,
        super.tooltip,
        super.elevation,
        super.shadowColor,
        super.surfaceTintColor,
        super.padding = const EdgeInsets.all(8.0),
        super.menuPadding,
        super.child,
        super.borderRadius,
        super.splashRadius,
        super.icon,
        super.iconSize,
        super.offset = Offset.zero,
        super.enabled = true,
        super.shape,
        super.color,
        super.iconColor,
        super.enableFeedback,
        super.constraints,
        super.position,
        super.clipBehavior = Clip.none,
        super.useRootNavigator = false,
        super.popUpAnimationStyle,
        super.routeSettings,
        super.style,
        super.requestFocus,
      });
    
      @override
      CustomPopupMenuButtonState<T> createState() =>
          CustomPopupMenuButtonState<T>();
    }
    
    class CustomPopupMenuButtonState<T> extends PopupMenuButtonState<T> {
      BuildContext? popupContext;
    
      bool isPopupOpening = false;
    
      RelativeRect? _cachedPosition;
    
      @override
      void dispose() {
        if (popupContext != null && isPopupOpening) {
          Navigator.pop(popupContext!);
        }
    
        super.dispose();
      }
    
      RelativeRect calculatePosition() {
        final popupMenuTheme = PopupMenuTheme.of(context);
        final button = context.findRenderObject() as RenderBox?;
    
        if (button == null) {
          return RelativeRect.fill;
        }
    
        final overlay = Navigator.of(
          context,
          rootNavigator: widget.useRootNavigator,
        ).overlay!.context.findRenderObject()! as RenderBox;
        final popupMenuPosition =
            widget.position ?? popupMenuTheme.position ?? PopupMenuPosition.over;
        late Offset offset;
        switch (popupMenuPosition) {
          case PopupMenuPosition.over:
            offset = widget.offset;
            break;
          case PopupMenuPosition.under:
            offset = Offset(0, button.size.height) + widget.offset;
            if (widget.child == null) {
              // Remove the padding of the icon button.
              offset -= Offset(0, widget.padding.vertical / 2);
            }
        }
        final position = RelativeRect.fromRect(
          Rect.fromPoints(
            button.localToGlobal(offset, ancestor: overlay),
            button.localToGlobal(
              button.size.bottomRight(Offset.zero) + offset,
              ancestor: overlay,
            ),
          ),
          Offset.zero & overlay.size,
        );
    
        return position;
      }
    
      RelativeRect _positionBuilder(BuildContext context, BoxConstraints _) {
        popupContext = context;
    
        try {
          _cachedPosition = calculatePosition();
          return _cachedPosition!;
        } catch (_) {
          return _cachedPosition ?? RelativeRect.fromSize(Rect.zero, Size.zero);
        }
      }
    
      @override
      void showButtonMenu() {
        final popupMenuTheme = PopupMenuTheme.of(context);
        final items = widget.itemBuilder(context);
    
        // Only show the menu if there is something to show
        if (items.isNotEmpty) {
          // cache the position
          _cachedPosition = calculatePosition();
    
          widget.onOpened?.call();
    
          isPopupOpening = true;
    
          showMenu<T?>(
            context: context,
            elevation: widget.elevation ?? popupMenuTheme.elevation,
            shadowColor: widget.shadowColor ?? popupMenuTheme.shadowColor,
            surfaceTintColor:
                widget.surfaceTintColor ?? popupMenuTheme.surfaceTintColor,
            items: items,
            initialValue: widget.initialValue,
            positionBuilder: _positionBuilder,
            shape: widget.shape ?? popupMenuTheme.shape,
            menuPadding: widget.menuPadding ?? popupMenuTheme.menuPadding,
            color: widget.color ?? popupMenuTheme.color,
            constraints: widget.constraints,
            clipBehavior: widget.clipBehavior,
            useRootNavigator: widget.useRootNavigator,
            popUpAnimationStyle: widget.popUpAnimationStyle,
            routeSettings: widget.routeSettings,
            requestFocus: widget.requestFocus,
          ).then<void>((T? newValue) {
            if (!mounted) {
              return null;
            }
    
            isPopupOpening = false;
    
            if (newValue == null) {
              widget.onCanceled?.call();
              return null;
            }
            widget.onSelected?.call(newValue);
          });
        }
      }
    }
  8. huycozy commented on Jul 22, 2025

    @huycozy
    Member

    For anyone who has encountered this issue: my PR #171970 was landed recently on master channel. Can you fetch master and retry to see if it fixes this issue too?

  9. keisei0214 commented on Jul 23, 2025

    @keisei0214

    I built and ran the code pointed out in this issue, and the error no longer occurs.
    After reviewing the changes, I also believe the error has been resolved.
    However, as shown in the attached video, the popup does not close when switching tabs.

    8f06783d5f578536f277ecf132aec76f608b98d6.mp4

    The version of Flutter I used is as follows:

    Flutter Version
    Flutter 3.33.0-1.0.pre-1064 • channel master • https://github.com/flutter/flutter.git
    Framework • revision db38e71bfc (33 hours ago) • 2025-07-22 10:16:48 +0700
    Engine • hash 8f06783d5f578536f277ecf132aec76f608b98d6 (revision db38e71bfc) (33 hours ago) • 2025-07-22 03:16:48.000Z
    Tools • Dart 3.10.0 (build 3.10.0-15.0.dev) • DevTools 2.48.0
    
  10. huycozy commented on Aug 11, 2025

    @huycozy
    Member

    @keisei0214 I'm not sure if my fix caused a regression issue or not. To clarify this, can you share a sample code within a separate issue for better tracking? Thanks!

  11. keisei0214 commented on Aug 15, 2025

    @keisei0214

    @huycozy
    I am not sure if this behavior has always been like this.
    However, with the current behavior, it is difficult to determine whether the popup is open, which I think affects usability.
    I have created the following ticket.
    Thank you.

    #173851

  12. huycozy commented on Aug 18, 2025

    @huycozy
    Member

    Per discussion in #173851, the exception no longer occurs, closing this as fixed. If anyone still encounters this issue, please write in comments or file a new issue.

  13. added
    r: fixedIssue is closed as already fixed in a newer version
    on Aug 18, 2025
  14. github-actions commented on Sep 1, 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.

  15. locked as resolved and limited conversation to collaborators on Sep 1, 2025
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

    P2Important issues not at the top of the work listfound in release: 3.29Found to occur in 3.29found in release: 3.31Found to occur in 3.31has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.r: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions