如何使用 url_launcher 在 flutter 应用程序中打开 URL?

问题描述

我使用url_launcher包打开url,代码如下:

  Future <void> _openURL(String url) async{
    if(await canLaunch(url)) {
      await launch(
          url,forceSafariVC: false,forceWebView: true,);
    }
    else{
      throw 'Cant open URL';
    }
  }


ListTile(
                        title: Text('Google'),onTap: () {
                          _openURL('https://www.google.de/');
                        }),

但是无论我想打开什么网址,我都会收到错误“无法打开网址”

解决方法

尝试以下代码:

创建launchURL函数:

_launch() async {
    const url = 'https://www.google.de/';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }

创建您的小部件:

ListTile(
     onTap: _launch,title: Text('Google'),),

希望它解决了你的问题

,

我收到同样的错误:未处理的异常:无法启动

正如您在此处看到的 https://pub.dev/packages/url_launcher,您必须将以下代码段放在您的 AndroidManifest.xml 中

<queries>
  <!-- If your app opens https URLs -->
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>
</queries>