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

升级flutter_inappwebview后出现命名参数未定义错误,求迁移解决方案

flutter_inappwebview版本升级API调整方案

核心原因

你遇到的参数未定义报错,是因为升级到了flutter_inappwebview 6.x及以上的大版本,该版本对原有API做了批量不兼容更新,对应修改规则如下:

各报错参数替换规则

  • initialHeadersRequest:已废弃,请求头直接配置到URLRequest的headers属性中即可
  • initialOptions:重命名为initialSettings,配套的InAppWebViewGroupOptions改为InAppWebViewSettings
  • debuggingEnabled:重命名为isInspectable
  • onWebViewCreated:重命名为onControllerReceived
  • onLoadStart:重命名为onPageStarted,回调参数url类型从String调整为Uri?
  • onLoadStop:重命名为onPageFinished,回调参数url类型从String调整为Uri?

修改后完整代码

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:gec_app/Menu.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
}

class AboutUs extends StatefulWidget {
  @override
  _AboutUsState createState() => new _AboutUsState();
}

class _AboutUsState extends State<AboutUs> {
  InAppWebViewController? webView;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
            title: const Text('Visit Us'),
            leading: new IconButton(
                icon: new Icon(Icons.arrow_back),
                onPressed: () {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => HomeScreen()));
                })),
        body: Container(
            child: Column(children: <Widget>[
              Expanded(
                  child: InAppWebView(
                    initialUrlRequest: URLRequest(
                      url: Uri.parse("https://www.gecgh.org/"),
                      headers: {},
                    ),
                    initialSettings: InAppWebViewSettings(
                      isInspectable: true,
                      preferredContentMode: UserPreferredContentMode.DESKTOP,
                    ),
                    onControllerReceived: (InAppWebViewController controller) {
                      webView = controller;
                    },
                    onPageStarted: (InAppWebViewController controller, Uri? url) {},
                    onPageFinished: (InAppWebViewController controller, Uri? url) async {},
                  ))
            ])),
      ),
    );
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 17:15:04