Repository: flutter
Issue: [webview_flutter] Non-null onNavigationRequest breaks window.location.reload() after navigation to an #anchor
Labels: platform-android,p: webview,package,team-ecosystem,has reproducible steps,P2,triaged-ecosystem,found in release: 3.16,found in release: 3.17

### 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

1. Run the code sample.
2. Click "Change text" so you can easily see when the page was reloaded (when the text gets changed back)
3. Click "Go to # anchor"
4. Click "Reload"

### Expected results

The website should reload.

### Actual results

The website does not reload, even though we receive a NavigationRequest which we fulfill with NavigationDecision.navigate.
This behaviour is different from how Chrome behaves. webview_flutter also behaves correctly when we don't specify a onNavigationRequest handler. However when we specify our basic onNavigationRequest handler that always returns NavigationDecision.navigate we experience this issue.

This might be caused by the same error that is causing https://github.com/flutter/flutter/issues/137737 since both are related to (not) providing an onNavigationRequest handler.

### Code sample

<details><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

import 'package:webview_flutter_android/webview_flutter_android.dart';
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';

void main() => runApp(const MaterialApp(home: WebViewExample()));

class WebViewExample extends StatefulWidget {
  const WebViewExample({Key? key}) : super(key: key);

  @override
  State<WebViewExample> createState() => _WebViewExampleState();
}

class _WebViewExampleState extends State<WebViewExample> {
  late final WebViewController _controller;

  @override
  void initState() {
    super.initState();

    final PlatformWebViewControllerCreationParams params;

    if (WebViewPlatform.instance is WebKitWebViewPlatform) {
      params = WebKitWebViewControllerCreationParams();
    } else if (WebViewPlatform.instance is AndroidWebViewPlatform) {
      params = AndroidWebViewControllerCreationParams();
    } else {
      params = const PlatformWebViewControllerCreationParams();
    }

    _controller = WebViewController.fromPlatformCreationParams(params);

    _controller.setJavaScriptMode(JavaScriptMode.unrestricted);
    _controller.setNavigationDelegate(NavigationDelegate(
      onNavigationRequest: (NavigationRequest request) {
        print("onNavigationRequest: ${request.url}");
        return NavigationDecision.navigate;
      },
    ));
    _controller.loadRequest(Uri.parse("https://webtoapp.design/reload_bug"));
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: WebViewWidget(controller: _controller),
    );
  }
}

```

This is the relevant code on the website:
```html
    <div class="container">
        <p id="text">This is the text after loading the page.</p>
        <button onclick="document.getElementById('text').innerHTML = 'Button was clicked.'">Change text</button>
        <br>
        <a href="#anchor">Go to #anchor</a>
        <br>
        <button onclick="window.location.reload()">Reload</button>
    </div>
```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details><summary>Logs</summary>

```console
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app-debug.apk...
Debug service listening on ws://127.0.0.1:52467/buUt3RLmodM=/ws
Syncing files to device sdk gphone64 x86 64...
I/WebViewFactory(11548): Loading com.google.android.webview version 113.0.5672.136 (code 567263637)
W/ziparchive(11548): Unable to open '/data/app/~~PN5oVJS-99NfuCDd2iFI8Q==/com.google.android.trichromelibrary_567263637-Y9-XvnW3vDJNm2snc3DlNg==/TrichromeLibrary.dm': No such file or directory
W/ziparchive(11548): Unable to open '/data/app/~~PN5oVJS-99NfuCDd2iFI8Q==/com.google.android.trichromelibrary_567263637-Y9-XvnW3vDJNm2snc3DlNg==/TrichromeLibrary.dm': No such file or directory
W/ple.webviewtest(11548): Entry not found
D/nativeloader(11548): Configuring clns-7 for other apk /data/app/~~PN5oVJS-99NfuCDd2iFI8Q==/com.google.android.trichromelibrary_567263637-Y9-XvnW3vDJNm2snc3DlNg==/TrichromeLibrary.apk. target_sdk_version=34, uses_libraries=ALL, library_path=/data/app/~~ja8zWhLc6SaAA6K8wDUsDw==/com.google.android.webview-Sp2f0l2sJWEsXTZ9hpswTQ==/lib/x86_64:/data/app/~~ja8zWhLc6SaAA6K8wDUsDw==/com.google.android.webview-Sp2f0l2sJWEsXTZ9hpswTQ==/WebViewGoogle.apk!/lib/x86_64:/data/app/~~PN5oVJS-99NfuCDd2iFI8Q==/com.google.android.trichromelibrary_567263637-Y9-XvnW3vDJNm2snc3DlNg==/TrichromeLibrary.apk!/lib/x86_64, permitted_path=/data:/mnt/expand
D/nativeloader(11548): Configuring clns-8 for other apk /data/app/~~ja8zWhLc6SaAA6K8wDUsDw==/com.google.android.webview-Sp2f0l2sJWEsXTZ9hpswTQ==/WebViewGoogle.apk. target_sdk_version=34, uses_libraries=, library_path=/data/app/~~ja8zWhLc6SaAA6K8wDUsDw==/com.google.android.webview-Sp2f0l2sJWEsXTZ9hpswTQ==/lib/x86_64:/data/app/~~ja8zWhLc6SaAA6K8wDUsDw==/com.google.android.webview-Sp2f0l2sJWEsXTZ9hpswTQ==/WebViewGoogle.apk!/lib/x86_64:/data/app/~~PN5oVJS-99NfuCDd2iFI8Q==/com.google.android.trichromelibrary_567263637-Y9-XvnW3vDJNm2snc3DlNg==/TrichromeLibrary.apk!/lib/x86_64, permitted_path=/data:/mnt/expand
W/ple.webviewtest(11548): Accessing hidden method Landroid/os/Trace;->isTagEnabled(J)Z (unsupported, reflection, allowed)
W/ple.webviewtest(11548): Accessing hidden method Landroid/os/Trace;->traceBegin(JLjava/lang/String;)V (unsupported, reflection, allowed)
W/ple.webviewtest(11548): Accessing hidden method Landroid/os/Trace;->traceEnd(J)V (unsupported, reflection, allowed)
W/ple.webviewtest(11548): Accessing hidden method Landroid/os/Trace;->asyncTraceBegin(JLjava/lang/String;I)V (unsupported, reflection, allowed)
W/ple.webviewtest(11548): Accessing hidden method Landroid/os/Trace;->asyncTraceEnd(JLjava/lang/String;I)V (unsupported, reflection, allowed)
I/cr_WVCFactoryProvider(11548): Loaded version=113.0.5672.136 minSdkVersion=29 isBundle=false multiprocess=true packageId=2
I/cr_VariationsUtils(11548): Failed reading seed file "/data/user/0/com.example.webviewtest/app_webview/variations_seed_new"
I/cr_VariationsUtils(11548): Failed reading seed file "/data/user/0/com.example.webviewtest/app_webview/variations_seed"
I/cr_LibraryLoader(11548): Successfully loaded native library
I/cr_CachingUmaRecorder(11548): Flushed 8 samples from 8 histograms.
D/CompatibilityChangeReporter(11548): Compat change id reported: 183155436; UID 10194; state: ENABLED
D/AutofillManager(11548): Fill dialog is enabled:false, hints=[password, passwordAuto, creditCardNumber, creditCardSecurityCode, creditCardExpirationDate]
W/chromium(11548): [WARNING:dns_config_service_android.cc(115)] Failed to read DnsConfig.
D/CompatibilityChangeReporter(11548): Compat change id reported: 214741472; UID 10194; state: ENABLED
D/CompatibilityChangeReporter(11548): Compat change id reported: 171228096; UID 10194; state: ENABLED
W/cr_media(11548): BLUETOOTH_CONNECT permission is missing.
W/cr_media(11548): registerBluetoothIntentsIfNeeded: Requires BLUETOOTH permission
I/PlatformViewsController(11548): Hosting view in view hierarchy for platform view: 0
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
D/CompatibilityChangeReporter(11548): Compat change id reported: 193247900; UID 10194; state: ENABLED
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
I/chromium(11548): [INFO:CONSOLE(0)] "Error with Permissions-Policy header: Origin trial controlled feature not enabled: 'interest-cohort'.", source:  (0)
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
D/EGL_emulation(11548): app_time_stats: avg=227.79ms min=42.17ms max=530.93ms count=7
D/EGL_emulation(11548): app_time_stats: avg=225.05ms min=30.86ms max=522.80ms count=6
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
D/EGL_emulation(11548): app_time_stats: avg=325.72ms min=102.22ms max=531.28ms count=5
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
D/CompatibilityChangeReporter(11548): Compat change id reported: 289878283; UID 10194; state: DISABLED
W/ple.webviewtest(11548): Accessing hidden method Landroid/view/MotionEvent;->getEventTimeNano()J (unsupported, reflection, allowed)
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
D/EGL_emulation(11548): app_time_stats: avg=377.37ms min=9.72ms max=4222.87ms count=12
D/EGL_emulation(11548): app_time_stats: avg=359.65ms min=6.76ms max=4201.08ms count=12
D/EGL_emulation(11548): app_time_stats: avg=754.99ms min=49.01ms max=4228.90ms count=6
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
D/EGL_emulation(11548): app_time_stats: avg=291.81ms min=10.87ms max=988.10ms count=4
D/EGL_emulation(11548): app_time_stats: avg=287.09ms min=8.32ms max=989.30ms count=4
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
D/EGL_emulation(11548): app_time_stats: avg=387.73ms min=53.78ms max=991.54ms count=3
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
I/flutter (11548): onNavigationRequest: https://webtoapp.design/reload_bug#anchor
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
D/EGL_emulation(11548): app_time_stats: avg=442.40ms min=18.72ms max=1232.90ms count=3
D/EGL_emulation(11548): app_time_stats: avg=442.35ms min=20.09ms max=1233.16ms count=3
E/FrameEvents(11548): updateAcquireFence: Did not find frame.
D/EGL_emulation(11548): app_time_stats: avg=666.92ms min=90.59ms max=1243.25ms count=2
E/FrameEvents(11548): updateAcquireFence: Did not find frame.

```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[√] Flutter (Channel stable, 3.16.1, on Microsoft Windows [Version 10.0.19045.3693], locale de-DE)
    • Flutter version 3.16.1 on channel stable at D:\Programme\Flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 7f20e5d18c (2 days ago), 2023-11-27 09:47:30 -0800
    • Engine revision 22b600f240
    • Dart version 3.2.1
    • DevTools version 2.28.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at D:\Programme\AndroidSDK
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME = D:\Programme\AndroidSDK
    • ANDROID_SDK_ROOT = D:\Programme\AndroidSDK
    • Java binary at: D:\Programme\AndroidStudio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    • All Android licenses accepted.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[!] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.7)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools
    • Visual Studio Build Tools 2019 version 16.11.31911.196
    • Windows 10 SDK version 10.0.19041.0
    X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components:
        MSVC v142 - VS 2019 C++ x64/x86 build tools
         - If there are multiple build tool versions available, install the latest
        C++ CMake tools for Windows
        Windows 10 SDK

[√] Android Studio (version 2022.3)
    • Android Studio at D:\Programme\AndroidStudio
    • 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-b2043.56-10027231)

[√] IntelliJ IDEA Community Edition (version 2020.2)
    • IntelliJ at D:\Programme\IntelliJ IDEA Community Edition 2020.2
    • 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

[√] Connected device (4 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 14 (API 34) (emulator)
    • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.19045.3693]
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 119.0.6045.160
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 119.0.2151.93

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

```

</details>