微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。