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

Flutter集成flutter_inappwebview后点击地图按钮显示错误页求解决

可行解决方案
  • 拦截URL Scheme/Intent跳转
    地图上的「使用应用」按钮通常是通过URL Scheme(如yourapp://xxx)或Android Intent唤起原生App,flutter_inappwebview默认不会处理这类跳转,需手动拦截:

    1. 添加url_launcher依赖,用于唤起外部URL。
    2. 在InAppWebView的shouldOverrideUrlLoading回调中判断并处理:
    InAppWebView(
      initialUrlRequest: URLRequest(url: Uri.parse("你的地图页面地址")),
      shouldOverrideUrlLoading: (controller, navigationAction) async {
        final uri = navigationAction.request.url!;
        // 拦截自定义Scheme
        if (uri.scheme == "yourapp") { // 替换为你的App的Scheme
          await launchUrl(uri);
          return NavigationActionPolicy.CANCEL; // 阻止WebView加载该URL
        }
        // 拦截Android Intent协议
        if (uri.toString().startsWith("intent://")) {
          try {
            final intent = Intent.parseUri(uri.toString(), IntentURIIntentParser());
            await intent.launch();
            return NavigationActionPolicy.CANCEL;
          } catch (_) {}
        }
        return NavigationActionPolicy.ALLOW;
      },
    )
    

    同时确保在Android的AndroidManifest.xml和iOS的Info.plist中正确配置对应的Scheme权限。

  • 开启WebView必要功能
    地图页面可能依赖JavaScript、本地存储等,需开启相关设置:

InAppWebView(
  initialSettings: InAppWebViewSettings(
    javaScriptEnabled: true,
    domStorageEnabled: true,
    allowFileAccess: true,
  ),
  // 其他配置
)
  • 自定义User-Agent
    部分地图服务会根据User-Agent判断运行环境,设置贴近原生浏览器的User-Agent可避免识别异常:
InAppWebView(
  initialSettings: InAppWebViewSettings(
    userAgent: "Mozilla/5.0 (Linux; Android 13) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Mobile Safari/537.36 YourApp/1.0.0",
  ),
)
  • 调试定位具体错误
    启用WebView调试模式,查看控制台日志定位问题:
    • Android:在AndroidManifest.xml的application标签添加android:debuggable="true",然后通过chrome://inspect调试WebView。
    • iOS:在Info.plist中添加NSAllowsArbitraryLoads(仅调试用),通过Safari开发者工具查看WebView控制台。

内容的提问来源于stack exchange,提问作者Ahmet Ekrem Hafız

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 00:50:13