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

使用url_launcher 6.1.5如何在默认浏览器打开URL?

解决url_launcher 6.1.5版本默认浏览器打开URL的问题

在url_launcher 6.1.5版本中,launch函数被弃用后,launchUrl通过LaunchUrlOptions的mode参数控制打开方式,具体实现步骤如下:

  1. 导入依赖包
    确保已导入url_launcher包:

    import 'package:url_launcher/url_launcher.dart';
    
  2. 调用launchUrl并指定外部应用模式
    使用LaunchMode.externalApplication模式,该模式会强制唤起系统默认外部应用(通常为默认浏览器)打开URL,而非应用内WebView:

    Future<void> openUrlInBrowser(Uri targetUrl) async {
      if (!await launchUrl(
        targetUrl,
        mode: LaunchMode.externalApplication,
      )) {
        throw Exception('无法打开URL: $targetUrl');
      }
    }
    
  3. 可选:其他模式说明
    若需要更灵活的打开策略,可选择其他LaunchMode枚举值:

    • LaunchMode.externalNonBrowserApplication:优先用对应URL的专属外部应用打开,无对应应用时 fallback 到浏览器
    • LaunchMode.platformDefault:遵循各平台默认的URL打开行为
    • LaunchMode.inAppWebView:强制在应用内WebView打开(此为你需要避免的模式)

该实现在Android和iOS平台均能稳定唤起系统默认浏览器打开目标URL。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 10:21:58