且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mailto:Flutter for Web的链接

更新时间:2023-11-26 18:51:04

稍作尝试后,我发现了一种使 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;
  }
}