【问题标题】:How to open URL in flutter app with url_launcher?如何使用 url_launcher 在 Flutter 应用程序中打开 URL?
【发布时间】:2021-10-07 03:01:44
【问题描述】:

我使用包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/');
                        }),

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

【问题讨论】:

  • 它适用于我的情况,您正在尝试哪种设备?

标签: flutter dart url url-launcher


【解决方案1】:

试试下面的代码:

创建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'),
    ),

希望它能解决你的问题

【讨论】:

  • 出了什么问题
  • 我得到同样的错误:未处理的异常:无法启动google.de
  • 尝试退出并重新启动应用程序或运行命令flutter clean 然后flutter pub get 然后运行您的应用程序
  • 还是不行,不知道怎么回事
【解决方案2】:

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

正如你在这里看到的https://pub.dev/packages/url_launcher,你必须在你的 AndroidManifest.xml 中加入以下 sn-p

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

【讨论】:

  • 我们应该在清单中的什么位置放置它?
猜你喜欢
  • 2022-09-29
  • 2022-12-29
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 2017-01-10
  • 2021-03-25
  • 2016-06-22
  • 2022-12-17
相关资源
最近更新 更多