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

Flutter中无法通过Link组件打开URL,Url Launcher启动异常

Url Launcher Link组件失效排查与修复方案

1. 检查依赖配置

确保pubspec.yaml中正确引入url_launcher依赖,并且执行过flutter pub get同步依赖:

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.2.5 # 建议使用最新稳定版本

2. 补充平台权限与配置

Android端

在android/app/src/main/AndroidManifest.xml中添加网络权限和链接跳转的Intent过滤:

<!-- 网络访问权限 -->
<uses-permission android:name="android.permission.INTERNET" />

<activity
    android:name=".MainActivity"
    ...>
    <!-- 允许处理http/https链接跳转 -->
    <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="http" />
        <data android:scheme="https" />
    </intent-filter>
</activity>

iOS端

在ios/Runner/Info.plist中添加允许访问http/https链接的配置:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>http</string>
    <string>https</string>
</array>

3. 修正组件使用方式

如果Link组件仍有兼容性问题,可直接使用url_launcher核心方法替代:

import 'package:url_launcher/url_launcher.dart';

// ...

final websiteUri = Uri.parse('https://flutter.dev');

TextButton(
  onPressed: () async {
    if (await canLaunchUrl(websiteUri)) {
      await launchUrl(websiteUri, mode: LaunchMode.externalApplication);
    }
  },
  child: Text(websiteUri.toString()),
)

4. 清理环境缓存

  • 执行flutter clean清理项目缓存
  • 重新运行flutter pub get和flutter run
  • 确认设备网络正常,可正常访问目标URL

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 12:40:33