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

Flutter异步函数正确定义及launch_url启动URL异常排查

解决Flutter中launch_url无法跳转网页的异常

问题重现

开发Flutter项目时,为"帮助与支持"模块实现网页跳转功能,使用launch_url包编写了异步跳转函数,但运行时持续抛出异常,提示无法启动目标URL。实体机和模拟器均存在该问题,相同代码在朋友环境中可正常运行。

实现代码

Future<void> _launchURL(Uri url) async {
  if (await canLaunchUrl(url)) {
    await launchUrl(url);
  } else {
    throw 'Could not launch $url';
  }
}

异常信息

I/UrlLauncher(30788): component name for https://flutter.dev/ is null
I/UrlLauncher(30788): component name for https://flutter.dev is null
E/flutter (30788): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Could not launch https://flutter.dev/
E/flutter (30788): #0      _launchURL (package:clothing_app/screens/settings.dart:385:5)
E/flutter (30788): <asynchronous suspension>
E/flutter (30788):

解决步骤

  • 检查AndroidManifest.xml配置
    在android/app/src/main/AndroidManifest.xml中,确认添加必要权限和Intent过滤器:

    <uses-permission android:name="android.permission.INTERNET" />
    
    <activity
        android:name=".MainActivity"
        ...>
        <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="https" />
        </intent-filter>
    </activity>
    
  • 更新包并清理缓存

    1. 在pubspec.yaml中确保launch_url包为最新版本,执行命令:flutter pub upgrade launch_url
    2. 清理项目缓存:flutter clean,之后重新运行项目:flutter run
  • 指定启动模式
    修改launchUrl调用,明确指定外部应用启动模式:

    await launchUrl(url, mode: LaunchMode.externalApplication);
    
  • 检查设备浏览器状态
    确认测试设备(模拟器/实体机)已安装可用浏览器,且默认浏览器设置正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 02:19:59