我如何将新的launchurl()方法与a mailto使用:&quot:关联?

发布于 2025-02-12 05:54:02 字数 2250 浏览 0 评论 0原文

我的应用程序中有一个小链接,上面写着“向我们发送电子邮件”。当您单击它时,我希望默认电子邮件应用程序打开,并通过电子邮件开始到我们的电子邮件地址。我曾经在url_launcher包中使用abought()方法的方法完全做到这一点:

import 'package:url_launcher/url_launcher.dart' as url;

Future<bool?> pressedSendUsEmail() async {
  bool? success;
  try {
    print('Pressed Send us an e-mail...');
    success = await url.launch('mailto:[email protected]');  // Works like a charm...
    print('success is $success');
  } catch (e) {
    print('Caught an error in Send us an e-mail!');
    print('e is: ${e.toString()}');
  }
  return success;
}

但是现在,我得到了警告,说abination()/代码>已弃用!我应该改用launchurl()。但是lainingurl()不采用字符串参数,它需要一个uri参数...而且我不知道如何正确编写此URI,以便它可以做我想要的!我尝试了:

  success = await url.launchUrl(Uri(path: 'mailto:[email protected]'));

但是这引发了一个错误,因为它无法解释“:”字符。我已经尝试过:

  success = await url.launchUrl(
    Uri.https('mailto:[email protected]', ''),
  );

这启动了链接,但是在浏览器中...并且它没有启动电子邮件到预先打印的地址。我尝试添加:

  success = await url.launchUrl(
    Uri.https('mailto:[email protected]', ''),
    mode: url.LaunchMode.externalApplication,
  );

这为我提供了一个可以打开链接的外部应用程序的选项,但是不幸的是,只列出了浏览器应用程序...而不是电子邮件应用程序!

我应该如何编写我的命令来制作lunageurl()只需执行旧启动()做的事情?最感谢帮助!


编辑:

在下面令人满意地回答了这个问题之后,我现在有一个后续QN:

在应用程序的另一部分中,在某个地方,用户可以在链接中输入链接,而我曾经用启动来启动它() ...还有一种简单的方法吗?

因为在这种情况下,我不知道该链接是否是HTTP或HTTPS,或者确实是Mailto:!我只希望它尝试按书面方式启动链接,只要它正确编写,它就会起作用。

I have a little link in my app that says "Send us an e-mail". When you click it, I want the default email app to open, with an email started to our email address. I used to make it do exactly that with the launch() method in the url_launcher package like this:

import 'package:url_launcher/url_launcher.dart' as url;

Future<bool?> pressedSendUsEmail() async {
  bool? success;
  try {
    print('Pressed Send us an e-mail...');
    success = await url.launch('mailto:[email protected]');  // Works like a charm...
    print('success is $success');
  } catch (e) {
    print('Caught an error in Send us an e-mail!');
    print('e is: ${e.toString()}');
  }
  return success;
}

But now, I get a warning saying launch() is deprecated! I should use launchUrl() instead. But launchUrl() doesn't take a String argument, it takes a Uri argument... and I don't know how to write this Uri correctly, so that it does what I want! I tried:

  success = await url.launchUrl(Uri(path: 'mailto:[email protected]'));

but that throws an error, because it can't interpret the ":" character. I've tried:

  success = await url.launchUrl(
    Uri.https('mailto:[email protected]', ''),
  );

and that launches the link, but in the browser... and it doesn't start up an e-mail to the pre-printed address. I tried adding:

  success = await url.launchUrl(
    Uri.https('mailto:[email protected]', ''),
    mode: url.LaunchMode.externalApplication,
  );

and that gives me an option of which external app to open the link with, but unfortunately, only browser apps are listed... not the email app!

How should I write my command to make the launchUrl() just do exactly what the old launch() did?? Most grateful for help!


Edit:

After that question was satisfactorily answered below, I now have a follow-up qn:

In another part of the app, there is a place where the user can type in a link, and I used to launch it with launch()... Is there a simple way to do that, as well?

Because in that case, I don't know if the link is gonna be a http or a https or indeed a mailto:!... and I would prefer not having to write lots of code to find that out! I just want it to try and launch the link exactly the way it's written, and so long as it's written correctly, it will work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开半夏魅人心 2025-02-19 05:54:02

尝试以下操作:

void _sendEmail(){
   final Uri emailLaunchUri = Uri(
     scheme: 'mailto',
     path: '[email protected]',
     queryParameters: {
      'subject': 'CallOut user Profile',
      'body': widget.userModel?.username ?? ''
     },
    );
   launchUrl(emailLaunchUri);
}

Try this:

void _sendEmail(){
   final Uri emailLaunchUri = Uri(
     scheme: 'mailto',
     path: '[email protected]',
     queryParameters: {
      'subject': 'CallOut user Profile',
      'body': widget.userModel?.username ?? ''
     },
    );
   launchUrl(emailLaunchUri);
}
回首观望 2025-02-19 05:54:02

使用已经内置的服务uri ...

void _sendEmail(){
    final Uri emailLaunchUri = Uri(
        scheme: 'mailto',
        path: '[email protected]',
        queryParameters: {
            'subject': 'CallOut user Profile',
            'body': widget.userModel?.username ?? ''
        },
    );
    launchUrl(emailLaunchUri);
}

Using the service already built-in to Uri...

void _sendEmail(){
    final Uri emailLaunchUri = Uri(
        scheme: 'mailto',
        path: '[email protected]',
        queryParameters: {
            'subject': 'CallOut user Profile',
            'body': widget.userModel?.username ?? ''
        },
    );
    launchUrl(emailLaunchUri);
}
染柒℉ 2025-02-19 05:54:02

内置查询构建器仅适用于HTTP/HTTPS。

对于其他任何内容,都应使用以下内容,否则您将拥有“+”而不是空格:

String? encodeQueryParameters(Map<String, String> params) {
  return params.entries
      .map((MapEntry<String, String> e) =>
          '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
      .join('&');
}
///
void _sendEmail() {
  final Uri emailLaunchUri = Uri(
    scheme: 'mailto',
    path: '[email protected]',
    query: encodeQueryParameters({
      'subject': 'CallOut user Profile',
      'body': widget.userModel?.username ?? ''
    }),
  );
  launchUrl(emailLaunchUri);
}

源: url_launcher文档

The built-in query builder only work correctly for http/https.

For anything else, the following should be used, otherwise you will have "+" instead of spaces:

String? encodeQueryParameters(Map<String, String> params) {
  return params.entries
      .map((MapEntry<String, String> e) =>
          '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
      .join('&');
}
///
void _sendEmail() {
  final Uri emailLaunchUri = Uri(
    scheme: 'mailto',
    path: '[email protected]',
    query: encodeQueryParameters({
      'subject': 'CallOut user Profile',
      'body': widget.userModel?.username ?? ''
    }),
  );
  launchUrl(emailLaunchUri);
}

Source: url_launcher Documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文