mailto:Flutter for Web的链接 [英] mailto: link for Flutter for Web

查看:80
本文介绍了mailto:Flutter for Web的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎没有url_launcher软件包( https://pub.dev/packages/url_launcher )为Flutter for Web工作.以下代码显示"test url1",但此后什么也没发生.

The url_launcher package (https://pub.dev/packages/url_launcher) doesn't seem to work for Flutter for Web. The following code prints "test url1" but nothing happens afterwards.

如何在Flutter for Web中实现类似 mailto:的功能,该功能导致默认电子邮件应用程序打开并带有预先填充的收件人:"电子邮件地址?

How can I implement mailto: like functionality in Flutter for Web which causes the default email app to open with a prepopulated 'to:' email address?

FlatButton(
  onPressed: _mailto, //() => {},
  padding: EdgeInsets.all(3.0),
  child: _contactBtn(viewportConstraints),
)


  _mailto() async {
    const url = 'mailto:support@email.com?subject=Product Inquiry&body=';
    print("test url1");
    if (await canLaunch(url)) {
      print("test url2");
      await launch(url);
    } else {
      print("test url3");
      throw 'Could not launch $url';
    }
  }

推荐答案

稍作尝试后,我发现了一种使 url_launcher 与网络兼容的方法.

After experimenting a little I found a way to make url_launcher work with web.

请勿使用 canLaunch(url).相反,只需使用 launch(url),然后将其包装在 try-catch 块中即可.这样一来,您应该很安全,并且电子邮件链接会起作用.为了赶上来,您可以仅将电子邮件复制到剪贴板,然后通过便条纸或薄纸通知用户.直到我们得到更好的解决方案,可能不是最好的解决方案,而是一个好的解决方案.

Don't use canLaunch(url). Instead, just use launch(url), but wrap it in try-catch block. This way you should be safe and the email link will work. For catch you can just copy the email to clipboard and notify the user about that with a snackbar or smth. Probably not the best solution, but a good one, till we get something better.

这是示例代码,以便您了解我的意思:

Here is the sample code, so that you see what I mean:

void _launchMailClient() async {
  const mailUrl = 'mailto:$kEmail';
  try {
    await launch(mailUrl);
  } catch (e) {
    await Clipboard.write(kEmail);
    _emailCopiedToClipboard = true;
  }
}

这篇关于mailto:Flutter for Web的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆