Repository: flutter
Issue: [webView_flutter] Flutter Webview can not be disposed in Flutter module
Labels: platform-android,platform-ios,p: webview,package,team-ecosystem,has reproducible steps,P2,found in release: 3.7,found in release: 3.9,triaged-ecosystem

I am working on a module type flutter application, when I go back to my native code doing "systemNavigator.pop" closing my Flutter page, when I reopen it I have two instances of webview in the safari development tools.

## Steps to Reproduce

1. Call the flutter view with webview from native
2. Close the flutter view
3. Call again the flutter view with webview from native
4. Check views on safari development tools

Expected results: Just on 1 webview's instance show up on safari development tools (as in android)

Actual results: 2 webview's instances show up on safari development tools (or those that appear when repeating the process)

Flutter: 3.3.10
Dart: 2.18.6
webview_flutter: 4.0.6
webview_flutter_android: 3.3.2
webview_flutter_wkwebview: 3.2.0
Xcode 14.2
iOS simulators and physical devices
iOS 16.2

<details>
<summary>Code sample</summary>

    import 'dart:io';
    import 'package:flutter/services.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_bloc/flutter_bloc.dart';
    import 'package:webview_cookie_manager/webview_cookie_manager.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';
    
    class Page extends StatefulWidget {
      const Page({Key? key}) : super(key: key);
    
      @override
      State<Page> createState() => Page();
    }
    
    class PageState extends State<Page> {
    
      final cookieManager = WebviewCookieManager();
      late final WebViewController _controller;
    
      @override
      void initState() {
        _initWebview();
    
        context.read<Bloc>().add(const DoGetDataURL());
    
        super.initState();
      }
    
      void _initWebview() async {
        // Enable virtual display.
        late final PlatformWebViewControllerCreationParams params;
        if (WebViewPlatform.instance is WebKitWebViewPlatform) {
          params = WebKitWebViewControllerCreationParams(
            allowsInlineMediaPlayback: true,
            mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
          );
        } else {
          params = const PlatformWebViewControllerCreationParams();
        }
        _controller = WebViewController.fromPlatformCreationParams(params)..setJavaScriptMode(JavaScriptMode.unrestricted);
    
        if (_controller.platform is AndroidWebViewController) {
          AndroidWebViewController.enableDebugging(true);
          (_controller.platform as AndroidWebViewController)
              .setMediaPlaybackRequiresUserGesture(false);
        }
      }
    
      // [Constructor]
      @override
      Widget build(BuildContext context) {
        
        final bloc = context.read<Bloc>();
    
        // Set WebView
        return BlocBuilder<Bloc, State>(
            buildWhen: (previous, current) => current is Loaded,
            builder: (context, state) {
              return Scaffold(
              appBar: _getHeader(context, state, bloc),
              body: _getPage(context, _getURL(state))
            );
          },
        );
      }
    
      // [Widgets]
      AppBar _getHeader(BuildContext context, State state, Bloc bloc) {
        return AppBar(
            leading: IconButton(
                onPressed: () => _goBackView(context, state),
                icon: const Icon(Icons.arrow_back_ios)));
      }
    
      Widget _getPage(BuildContext context, String currentURL) {
        if (currentURL.isEmpty) {
          return const Scaffold();
        } else {
          return WebViewWidget(
            controller: _controller..loadRequest(Uri.parse(currentURL))
              ..setNavigationDelegate(NavigationDelegate(
                onPageStarted: (url) => HttpUtil().setCookies(currentURL, cookieManager),
              )
            )  
          );
        }
      }
    
      // [Methods]
      void _goBackView(BuildContext context, State state) async {
        
        List<Cookie> _gotCookies = await cookieManager.getCookies(_getURL(state));
        context.read<Bloc>().add(DoSendCookiesToNative(cookiesList: _gotCookies));
        
        Navigator.pop(context);
        
        Navigator.pushNamed(context, Routes.whiteRoute);
    
        Future.delayed(const Duration(milliseconds: 250), () => SystemNavigator.pop(animated: true));
      }
    
      // [Functions]
      String _getURL(State state) {
        return state is Loaded ? state.url : '';
      }
    }

</details>

<details>
<summary>Flutter Doctor</summary>

    [✓] Flutter (Channel stable, 3.3.10, on macOS 13.2.1 22D68 darwin-arm, locale
        es-CO)
        • Flutter version 3.3.10 on channel stable at
          /Users/teguii/Documents/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 135454af32 (3 months ago), 2022-12-15 07:36:55 -0800
        • Engine revision 3316dd8728
        • Dart version 2.18.6
        • DevTools version 2.15.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
        • Android SDK at /Users/teguii/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0
        • Java binary at: /Applications/Android
          Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build
          11.0.15+0-b2043.56-8887301)
        • 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.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2022.1)
        • 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
          11.0.15+0-b2043.56-8887301)
    
    [✓] VS Code (version 1.76.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.60.0
    
    [✓] Connected device (3 available)
        • iPad Pro (12.9-inch) (6th generation) (mobile) •
          3A85C39B-67FE-4DCE-8828-184B3C0C7E1A • ios            •
          com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator)
        • macOS (desktop)                                • macos
          • darwin-arm64   • macOS 13.2.1 22D68 darwin-arm
        • Chrome (web)                                   • chrome
          • web-javascript • Google Chrome 111.0.5563.64
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!

</details>

this issue is closely related to this: https://github.com/flutter/flutter/issues/118579