Repository: flutter
Issue: [go_router] Accessing GoRouterState in NavigationObserver after showMenu causes application to hang
Labels: package,has reproducible steps,P2,p: go_router,team-go_router,triaged-go_router,found in release: 3.24,found in release: 3.27

### Steps to reproduce


1. Setup GoRouter using a ShellRoute and add a NavigationObserver that uses ```GoRouterState.of(route.navigator!.context)```
2. add call to ```showMenu()``` in app
3. Line ```GoRouterState.of(route.navigator!.context)``` causes application to hang

Have noticed after setting up sample code that you can experience the same error if it tries to do this on the root Route.

Note this doesn't hang or crash on version 14.2.0, only noticed after updating to 14.6.0.  Previoulsy there was an exception that we handled.


### Expected results

Exception occurs but application can recover.

### Actual results

Application hangs and doesn't recover

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

// GoRouter configuration
final _router = GoRouter(
  routes: [
    ShellRoute(
      builder: (context, state, child) => child,
      observers: [TestObserver()],
      routes: [
        GoRoute(
          path: '/',
          builder: (context, state) => const MyHomePage(title: ''),
        ),
      ],
    ),
  ],
);

class TestObserver extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPush(route, previousRoute);

    // NOTE: removing this line will cause application to hang on startup
    if (route.settings.name == '/') {
      return;
    }

    var state = route.settings.arguments is GoRouterState ? route.settings.arguments as GoRouterState? : null;
    if (state == null) {
      if (route.navigator != null) {
        try {
          debugPrint(
              'Attempting get GoRouterState - Route: ${route.settings.name}, ${route.navigator!.context.mounted}');
          state = GoRouterState.of(route.navigator!.context);
        } catch (e, st) {
          debugPrint('Error: $e\n$st');
          return;
        }
      }
    }
  }
}

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(routerConfig: _router);
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: InkWell(
          onTap: () => _showMenu(context),
          child: const Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'Tap Here',
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _showMenu(context),
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  void _showMenu(BuildContext context) {
    showMenu(context: context, position: const RelativeRect.fromLTRB(0, 0, 0, 0), items: [
      const PopupMenuItem<int>(
        value: 0,
        child: Text('First'),
      ),
      const PopupMenuItem<int>(
        value: 1,
        child: Text('Second'),
      ),
    ]);
  }
}



```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details open><summary>Logs</summary>

```console
Launching lib\main.dart on Chrome in debug mode...
This app is linked to the debug service: ws://127.0.0.1:50798/PJe3qvrOcvY=/ws
Debug service listening on ws://127.0.0.1:50798/PJe3qvrOcvY=/ws
Connecting to VM Service at ws://127.0.0.1:50798/PJe3qvrOcvY=/ws
Connected to the VM Service.
Attempting get GoRouterState - Route: null, true
Application finished.

Exited (-1).
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[√] Flutter (Channel stable, 3.24.5, on Microsoft Windows [Version 10.0.22631.4460], locale en-GB)
    • Flutter version 3.24.5 on channel stable at C:\sdks\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision dec2ee5c1f (2 weeks ago), 2024-11-13 11:13:06 -0800
    • Engine revision a18df97ca5
    • Dart version 3.5.4
    • DevTools version 2.37.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at C:\Users\pchar\AppData\Local\Android\Sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.42)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
    • Visual Studio Build Tools 2019 version 16.11.35425.106
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2024.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • 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.3+-12282718-b509.11)

[√] VS Code (version 1.95.3)
    • VS Code at C:\Users\pchar\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.100.0

[√] Connected device (4 available)
    • FP4 (mobile)      • 5e864742 • android-arm64  • Android 13 (API 33)
    • Windows (desktop) • windows  • windows-x64    • Microsoft Windows [Version 10.0.22631.4460]
    • Chrome (web)      • chrome   • web-javascript • Google Chrome 131.0.6778.86
    • Edge (web)        • edge     • web-javascript • Microsoft Edge 131.0.2903.70

[√] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>