Repository: flutter
Issue: ModalRoute.of(navigatorKey.currentContext!)! is always null
Labels: framework,f: routes,has reproducible steps,P3,team-framework,triaged-framework,found in release: 3.13,found in release: 3.14

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

ModalRoute.of(navigatorKey.currentContext!)! is null

### Expected results

Should give route name

### Actual results

null

### Code sample

[Try on Zapp](https://zapp.run/edit/flutter-zo6062co706?entry=lib/main.dart&file=lib/main.dart:0-2322)

<details><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

final navigatorKey = GlobalKey<NavigatorState>();

void main() {
  runApp(const App());
}

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      navigatorKey: navigatorKey,
      routes: {
        '/': (context) => HomePage(),
        '/detail': (context) => DetailPage(),
      },
    );
  }
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ElevatedButton(
              child: Text('Navigat to Detail'),
              onPressed: () {
                navigatorKey.currentState!.pushNamed('/detail');
              },
            ),
            ElevatedButton(
              child: Text('Wigdet Context'),
              onPressed: () {
                final routeName = ModalRoute.of(context)!.settings.name!;
                print(routeName);
              },
            ),
            ElevatedButton(
              child: Text('NavigatorKey Context'),
              onPressed: () {
                final routeName =
                    ModalRoute.of(navigatorKey.currentContext!)!.settings.name!;
                print(routeName);
              },
            ),
          ],
        ),
      ),
    );
  }
}

class DetailPage extends StatelessWidget {
  const DetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ElevatedButton(
              child: Text('Wigdet Context'),
              onPressed: () {
                final routeName = ModalRoute.of(context)!.settings.name!;
                print(routeName);
              },
            ),
            ElevatedButton(
              child: Text('NavigatorKey Context'),
              onPressed: () {
                final routeName =
                    ModalRoute.of(navigatorKey.currentContext!)!.settings.name!;
                print(routeName);
              },
            ),
          ],
        ),
      ),
    );
  }
}
```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.13.2, on macOS 14.0 23A5328b darwin-arm64, locale en-US)
    • Flutter version 3.13.2 on channel stable at /Users/burhankhanzada/Development/Google/Flutter/fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ff5b5b5fa6 (9 days ago), 2023-08-24 08:12:28 -0500
    • Engine revision b20183e040
    • Dart version 3.1.0
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/burhankhanzada/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
    • Xcode at /Applications/Xcode-beta.app/Contents/Developer
    • Build 15A5160n
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • 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 17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.81.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.70.0

[✓] Connected device (3 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 14 (API 34) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 14.0 23A5328b darwin-arm64
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 116.0.5845.140

[✓] Network resources
    • All expected network resources are available.

• No issues found!```

</details>