如何让 url 启动器发送电子邮件?

Adr*_*ope 4 dart flutter flutter-dependencies

我希望有人能够单击“联系我”按钮,并让该按钮打开包含我的电子邮件的电子邮件客户端。我目前仅使用他们在 url_launcher 5.4.11 包上提供的虚拟数据。

\n

当我的网址为“https://flutter.dev”时,此功能可以正常工作;但当我尝试输入自定义支持的 URL 方案来发送电子邮件时不起作用。

\n

我收到的错误是“未处理的异常:无法启动 smith@example.org?subject=Terra%20Tarot%20Question%20or%20Feedback\n#0 _launchURL (package:tarotcards/screens/support.dart:11:5) \n"

\n

我在某处读到,无法在 iOS 模拟器上调用邮件应用程序。这是真的?

\n

问题出在 _launchURL() 方法中的 url。\n如果您能帮助我解决此问题,我们将不胜感激。非常感谢!

\n
import 'package:flutter/cupertino.dart';\nimport 'package:flutter/material.dart';\nimport 'package:url_launcher/url_launcher.dart';\n\n_launchURL() async {\n  const url = 'mailto:smith@example.org?subject=News&body=New%20plugin';\n  if (await canLaunch(url)) {\n    await launch(url);\n  } else {\n    throw 'Could not launch $url';\n  }\n}\n\nclass Support extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) {\n    return Scaffold(\n      body: SafeArea(\n        child: Column(\n          children: <Widget>[\n            Image.asset('images/skull.png'),\n            Container(\n              padding: EdgeInsets.fromLTRB(0, 20.0, 0, 10.0),\n              child: Text(\n                'Need some help?',\n                style: TextStyle(\n                    color: Colors.white,\n                    fontFamily: 'Opensans',\n                    fontSize: 25.0,\n                    fontWeight: FontWeight.bold),\n              ),\n            ),\n            Container(\n              padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 0.0),\n              child: Text(\n                'Experiencing bugs? Want to request a feature?'\n                'Feel free to get in touch with me.'\n                ' ~Adriana',\n                style: TextStyle(\n                    color: Colors.white,\n                    letterSpacing: 0.5,\n                    height: 1.5,\n                    fontFamily: 'Opensans',\n                    fontSize: 16.0,\n                    fontWeight: FontWeight.normal),\n              ),\n            ),\n            SizedBox(\n              height: 40.0,\n            ),\n            ButtonTheme(\n              //TODO:// make button #1 work!\n              minWidth: 300.0,\n              child: RaisedButton(\n                child: Text('CONTACT ME'),\n                padding: EdgeInsets.all(10.0),\n                textColor: Colors.white,\n                color: Color(0XFFeb1555),\n                onPressed: _launchURL,\n              ),\n            ),\n            SizedBox(\n              height: 40.0,\n            ),\n            ButtonTheme(\n              //TODO:// make button #2 work!\n              minWidth: 300.0,\n              child: RaisedButton(\n                child: Text('FREQUENTLY ASKED QUESTIONS'),\n                padding: EdgeInsets.all(10.0),\n                textColor: Colors.black,\n                color: Colors.white,\n                onPressed: () {\n                  print('second button tapped');\n                },\n              ),\n            ),\n            SizedBox(\n              height: 150.0,\n            ),\n            FlatButton(\n              child: Text(\n                'Terms of Service & Privacy Policy \xe2\x86\x92',\n                style: TextStyle(\n                  color: Color(0XFFeb1555),\n                ),\n              ),\n              onPressed: () {\n                print('privacy policy tapped');\n              },\n            ),\n          ],\n        ),\n      ),\n    );\n  }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

小智 7

我无法在真实设备的机器上重现该错误!

你用模拟器吗?请记住,模拟器无法处理 mailto,因为邮件应用程序不可用。

尝试:

_launchURL() async {
  final url =
      Uri.encodeFull('mailto:smith@example.org?subject=News&body=New plugin');
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以尝试这个包,而不是在真实设备上: flutter_email_sender