Repository: flutter
Issue: Strange behaviour of Navigator
Labels: framework,f: routes,has reproducible steps,P2,team-framework,triaged-framework,found in release: 3.13,found in release: 3.16

### 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)

### Update

See this [comment](https://github.com/flutter/flutter/issues/136245#issuecomment-1835722181) 

---

### Steps to reproduce

Create a new flutter project.
Add a Scaffold widget and make a demo screen with a button.
Wrap scaffold with WillPopScope.
In my case, I added Exit Confirmation on the onWillPop callback.

Error 1. On click of that button add `Navigator.of(context).pop()` and click it.
Error 2. On click of that button add `Navigator.of(context).maybePop()` and click it.

Now If I press Android's physical back button same `onWillPop` function is called and if I return true there app closes.
Only manual pop does not work.

### Expected results

Expected results for Error 1: App Should close as there were no entries in the stack.

Expected results for Error 2: We should see exit confirmation and after returning true from onWillPop app should close.

### Actual results

### In Error 1:

The app shows a black screen.
After that, If I hot reload the app then I see a red error screen.

<img src="https://github.com/flutter/flutter/assets/114082175/53ad01d8-cf74-4d4c-bd0f-49f5c566122b" alt="drawing" width="200"/>


### In Error 2:

Nothing happens after returning true from `onWillPop` callback.


### Code sample

<details><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(home: HomeScreen()));
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});
  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => await showConfirmationDialog(context) ?? false,
      child: Scaffold(
        body: Center(
          child: ElevatedButton(
            onPressed: Navigator.of(context).pop,
            // onPressed: Navigator.of(context).maybePop,
            child: const Text('Exit'),
          ),
        ),
      ),
    );
  }
}

Future<bool?> showConfirmationDialog(BuildContext context) {
  return showDialog<bool>(
    context: context,
    builder: (ctx) => AlertDialog(
      content: const Text('Do you want to exit?'),
      actions: [
        ElevatedButton(
          onPressed: () => Navigator.of(context).pop(true),
          child: const Text('Yes'),
        ),
        ElevatedButton(
          onPressed: Navigator.of(context).pop,
          child: const Text('No'),
        ),
      ],
    ),
  );
}

```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

_No response_

### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.13.6, on macOS 12.7 21G816 darwin-x64, locale en-IN)
    • Flutter version 3.13.6 on channel stable at /Users/os/Developer/sdk/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ead455963c (13 days ago), 2023-09-26 18:28:17 -0700
    • Engine revision a794cf2681
    • Dart version 3.1.3
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/os/Library/Android/sdk
    • Platform android-33, build-tools 33.0.2
    • ANDROID_HOME = /Users/os/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/os/Library/Android/sdk
    • 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 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • 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.83.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.74.0

[✓] Connected device (4 available)
    • SM A525F (mobile)          • 192.168.29.63:5555                   • android-arm64  • Android 13 (API 33)
    • iPhone 14 Pro Max (mobile) • 80B1843E-51B5-40A2-99FA-59D93E9E5FF0 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator)
    • macOS (desktop)            • macos                                • darwin-x64     • macOS 12.7 21G816 darwin-x64
    • Chrome (web)               • chrome                               • web-javascript • Google Chrome 117.0.5938.149

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>