Repository: flutter
Issue: [webview_flutter] onWebResourceError is invoked when calling goBack
Labels: platform-ios,p: webview,package,has reproducible steps,P3,found in release: 3.3,found in release: 3.4,team-ios,triaged-ios


## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1. Execute `flutter run` on the code sample <!-- (see "Code sample" section below) -->
2. The GitHub page will appear, so go to any page.
3. Tap the back arrow in the lower left corner of the screen.
4. An error (The operation couldn't be completed.) is output to the console.

**Expected results:** <!-- what did you want to see? -->
"The operation couldn't be completed." does not occur.

**Actual results:** <!-- what did you see? -->
An error occurs in goBack and onWebResourceError is called.

<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```dart
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late WebViewController _webViewController;
  bool _isLoading = true;
  bool _canGoBack = false;
  bool _canGoForward = false;
  double _downloadPercentage = 0.0;
  String _title = '';

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) WebView.platform = AndroidWebView();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_title),
      ),
      body: Column(
        children: [
          _isLoading
              ? LinearProgressIndicator(
                  value: _downloadPercentage,
                  minHeight: 7.0,
                )
              : const SizedBox(),
          Expanded(
            child: Scrollbar(
              trackVisibility: true,
              child: WebView(
                initialUrl: 'https://github.com/',
                javascriptMode: JavascriptMode.unrestricted,
                onWebViewCreated: (controller) {
                  _webViewController = controller;
                },
                onPageFinished: (String url) async {
                  final title = await _webViewController.getTitle();
                  if (title != null) _title = title;

                  _canGoBack = await _webViewController.canGoBack();
                  _canGoForward = await _webViewController.canGoForward();

                  if (_isLoading) _toggleLoading();
                },
                onProgress: (progress) => _calculateProgress(progress),
                onWebResourceError: (error) async {
                  print(
                    'errorCode=${error.errorCode}, domain=${error.domain}, description=${error.description}, errorType=${error.errorType}, failingUrl=${error.failingUrl}',
                  );
                },
              ),
            ),
          ),
        ],
      ),
      persistentFooterAlignment: AlignmentDirectional.center,
      persistentFooterButtons: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            IconButton(
              icon: const Icon(Icons.arrow_back),
              onPressed: _canGoBack ? _webViewController.goBack : null,
            ),
            IconButton(
              icon: const Icon(Icons.arrow_forward),
              onPressed: _canGoForward ? _webViewController.goForward : null,
            ),
          ],
        ),
      ],
    );
  }

  void _toggleLoading() {
    setState(() => _isLoading = !_isLoading);
  }

  void _calculateProgress(int progress) {
    setState(() => _downloadPercentage = (progress / 100));
  }
}

```

</details>

<details>
  <summary>Logs</summary>

```
Launching lib/main.dart on motucraft in debug mode...
Automatically signing iOS for device deployment using specified development team in Xcode project: PNRW3X92CC
Running pod install...
Running Xcode build...
Xcode build done.                                           15.8s
Installing and launching...
[VERBOSE-2:FlutterObservatoryPublisher.mm(101)] Failed to register observatory port with mDNS with error -65555.
[VERBOSE-2:FlutterObservatoryPublisher.mm(103)] On iOS 14+, local network broadcast in apps need to be declared in the app's Info.plist. Debug and profile Flutter apps and modules host VM services on the local network to support debugging features such as hot reload and DevTools. To make your Flutter app or module attachable and debuggable, add a '_dartobservatory._tcp' value to the 'NSBonjourServices' key in your Info.plist for the Debug/Profile configurations. For more information, see https://flutter.dev/docs/development/add-to-app/ios/project-setup#local-network-privacy-permissions
[VERBOSE-2:FlutterObservatoryPublisher.mm(101)] Failed to register observatory port with mDNS with error -65555.
[VERBOSE-2:FlutterObservatoryPublisher.mm(103)] On iOS 14+, local network broadcast in apps need to be declared in the app's Info.plist. Debug and profile Flutter apps and modules host VM services on the local network to support debugging features such as hot reload and DevTools. To make your Flutter app or module attachable and debuggable, add a '_dartobservatory._tcp' value to the 'NSBonjourServices' key in your Info.plist for the Debug/Profile configurations. For more information, see https://flutter.dev/docs/development/add-to-app/ios/project-setup#local-network-privacy-permissions
Debug service listening on ws://127.0.0.1:53972/NetNoqfwW10=/ws
Syncing files to device motucraft...
[Security] This method should not be called on the main thread as it may lead to UI unresponsiveness.
[Security] This method should not be called on the main thread as it may lead to UI unresponsiveness.
[Security] This method should not be called on the main thread as it may lead to UI unresponsiveness.
[Security] This method should not be called on the main thread as it may lead to UI unresponsiveness.
[Loading] 0x13b01a218 - [pageProxyID=6, webPageID=7, PID=21785] WebPageProxy::didFailLoadForFrame: frameID=3, isMainFrame=1, domain=NSURLErrorDomain, code=-999
flutter: errorCode=-999, domain=NSURLErrorDomain, description=The operation couldn’t be completed. (NSURLErrorDomain error -999.), errorType=null, failingUrl=null
```

pubspec.yml
```
name: webview
description: A new Flutter project.

publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=2.18.1 <3.0.0'

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  webview_flutter: ^3.0.4

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^2.0.0

flutter:
  uses-material-design: true

```

flutter analyze
```
% flutter analyze
Running "flutter pub get" in webview...                            357ms
Analyzing webview...                                                    

   info • Avoid `print` calls in production code • lib/main.dart:87:19 • avoid_print

1 issue found. (ran in 3.5s)
```

flutter doctor -v
```
% flutter doctor -v
[✓] Flutter (Channel stable, 3.3.2, on macOS 12.6 21G115 darwin-arm, locale ja-JP)
    • Flutter version 3.3.2 on channel stable at /Users/osaki/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e3c29ec00c (13 days ago), 2022-09-14 08:46:55 -0500
    • Engine revision a4ff2c53d8
    • Dart version 2.18.1
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/osaki/Library/Android/sdk
    • Platform android-33, build-tools 32.1.0-rc1
    • Java binary at: /Users/osaki/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9014738/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Users/osaki/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/212.5712.43.2112.8815526/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 11.0.12+0-b1504.28-7817840)

[✓] Android Studio (version 2021.3)
    • Android Studio at /Users/osaki/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9014738/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 11.0.13+0-b1751.21-8125866)

[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.2)
    • IntelliJ at /Users/osaki/Applications/JetBrains Toolbox/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 70.0.5
    • Dart plugin version 222.4167.21

[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.2)
    • IntelliJ at /Users/osaki/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/222.4167.29/IntelliJ IDEA.app
    • 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

[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.1)
    • IntelliJ at /Users/osaki/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/222.3739.54/IntelliJ IDEA.app
    • 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

[✓] VS Code (version 1.71.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (4 available)
    • SC 02M (mobile)    • RF8MA2FM5PE               • android-arm64  • Android 11 (API 30)
    • motucraft (mobile) • 00008110-000679041A81801E • ios            • iOS 16.0.2 20A380
    • macOS (desktop)    • macos                     • darwin-arm64   • macOS 12.6 21G115 darwin-arm
    • Chrome (web)       • chrome                    • web-javascript • Google Chrome 105.0.5195.125

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>



https://user-images.githubusercontent.com/35750184/192419292-d220ad38-4bc1-42ec-a631-d8e258db96c7.mp4