mailto:Flutter for Web 的链接

Jas*_* O. 6 mailto flutter

url_launcher 包 ( https://pub.dev/packages/url_launcher ) 似乎不适用于 Flutter for Web。以下代码打印“test url1”,但之后没有任何反应。

我如何mailto:在 Flutter for Web 中实现类似的功能,这会导致默认电子邮件应用程序以预先填充的“to:”电子邮件地址打开?

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';
    }
  }
Run Code Online (Sandbox Code Playgroud)

Max*_*mer 7

经过一些试验后,我找到了一种使url_launcher与 web协同工作的方法。

不要使用canLaunch(url). 相反,只需使用launch(url),但将其包装在try-catch块中。这样你应该是安全的,电子邮件链接将起作用。对于捕获,您只需将电子邮件复制到剪贴板,并使用小吃店或 smth 通知用户。可能不是最好的解决方案,而是一个好的解决方案,直到我们得到更好的解决方案。

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

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