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

Flutter WebView无法打开外部链接,报net::ERR_UNKNOWN_URL_SCHEME错误

解决Flutter WebView中mailto/tel跳转的net::ERR_UNKNOWN_URL_SCHEME错误

问题根源

你的代码存在两个关键问题:

  1. shouldOverrideUrlLoading方法的返回值类型错误,且最终返回false不符合方法要求的NavigationDecision枚举类型
  2. 对mailto/tel链接的处理逻辑冗余,错误截取路径可能导致跳转链接无效

修正后的代码

class MyNavigationDelegate extends NavigationDelegate {
  @override
  Future<NavigationDecision> shouldOverrideUrlLoading(
      NavigationRequest navigation) async {
    final uri = Uri.parse(navigation.url);

    // 处理mailto和tel协议
    if (uri.scheme == 'mailto' || uri.scheme == 'tel') {
      // 直接使用原始链接跳转,无需重新拼接
      await launchUrl(uri);
      // 阻止WebView加载该链接,避免触发错误
      return NavigationDecision.prevent;
    }

    // 其他协议链接交给WebView正常加载
    return NavigationDecision.navigate;
  }
}

class _MyHomePageState extends State<MyHomePage> {
  late final WebViewController controller;

  @override
  void initState() {
    super.initState();
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setNavigationDelegate(MyNavigationDelegate())
      ..loadRequest(Uri.parse("你的实际URL"));
    addFileSelectionListener();
  }
}

额外注意事项

  • 确保url_launcher依赖已正确配置,在pubspec.yaml中添加最新版本并执行flutter pub get
  • Android平台需在AndroidManifest.xml中添加对应intent过滤器(可选,增强系统兼容性):
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="mailto" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="tel" />
    </intent-filter>
    
  • iOS平台无需额外配置,url_launcher会自动处理mailto/tel协议跳转

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 04:00:01