You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter SDK2.10.4中WebView点击TextFormField重载报错求解决方案

问题描述

我正在开发基于Flutter SDK 2.10.4的应用,已集成包含表单字段的WebView页面。点击TextFormField时,WebView会重复重载,且出现错误提示:

WebView.destroy() called while WebView is still attached to window.

尝试过更换WebView包、升级SDK等方案均无效;将项目SDK版本升级至2.18.0后,全项目所有文件均出现Null Safety错误。请问是否存在无需升级SDK即可修复WebView问题的方案?

Flutter Doctor 输出

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.3.9, on Microsoft Windows [Version 10.0.22621.1702], locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
[√] Chrome - develop for the web
[X] Visual Studio - develop for Windows
    X Visual Studio not installed; this is necessary for Windows development.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2021.3)
[√] Connected device (4 available)
[√] HTTP Host Availability

! Doctor found issues in 2 categories.
无需升级SDK的修复方案

以下是几个针对该问题的可行修复手段,均无需升级Flutter SDK:

  • 禁用WebView硬件加速
    在Android平台配置中关闭WebView硬件加速,避免渲染冲突引发的销毁异常。操作方式:
    打开android/app/src/main/AndroidManifest.xml,在对应<activity>标签内添加属性:

    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:hardwareAccelerated="false"
        ...>
    
  • 手动管控WebView生命周期
    避免系统自动销毁WebView,通过控制器手动管理生命周期。以webview_flutter包为例:

    WebViewController? _controller;
    
    @override
    void initState() {
      super.initState();
      _controller = WebViewController()
        ..loadRequest(Uri.parse("你的网页地址"));
    }
    
    @override
    void dispose() {
      // 先清空缓存并加载空白页,再执行销毁
      _controller?.clearCache();
      _controller?.loadRequest(Uri.parse("about:blank"));
      super.dispose();
    }
    
  • 调整TextField焦点与WebView的显示逻辑
    通过焦点监听,在TextField获取焦点时暂时隐藏WebView,失去焦点后恢复显示:

    final FocusNode _textFieldFocus = FocusNode();
    bool _showWebView = true;
    
    @override
    void initState() {
      super.initState();
      _textFieldFocus.addListener(() {
        setState(() {
          _showWebView = !_textFieldFocus.hasFocus;
        });
      });
    }
    
    @override
    Widget build(BuildContext context) {
      return Column(
        children: [
          TextFormField(focusNode: _textFieldFocus),
          _showWebView ? WebView(controller: _controller!) : SizedBox.shrink(),
        ],
      );
    }
    
  • 回退到兼容的WebView包版本
    若之前更换的是高版本WebView包,尝试回退到与Flutter 2.10.4适配性更好的旧版本,例如webview_flutter: 3.0.4,修改pubspec.yaml:

    dependencies:
      webview_flutter: ^3.0.4
    
  • 修复Android许可问题
    先执行命令flutter doctor --android-licenses接受所有Android许可,部分WebView异常可能与许可未完全配置有关。

内容的提问来源于stack exchange,提问作者Aqeel Mughal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 21:07:17