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

标签 flutter dart flutter-dependencies

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

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

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

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

问题在于我的 _launchURL() 方法中的 url。 如果你能帮我解决这个问题,那将不胜感激。非常感谢!

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

_launchURL() async {
  const url = 'mailto:smith@example.org?subject=News&body=New%20plugin';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

class Support extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Image.asset('images/skull.png'),
            Container(
              padding: EdgeInsets.fromLTRB(0, 20.0, 0, 10.0),
              child: Text(
                'Need some help?',
                style: TextStyle(
                    color: Colors.white,
                    fontFamily: 'Opensans',
                    fontSize: 25.0,
                    fontWeight: FontWeight.bold),
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 0.0),
              child: Text(
                'Experiencing bugs? Want to request a feature?'
                'Feel free to get in touch with me.'
                '🔮 ~Adriana',
                style: TextStyle(
                    color: Colors.white,
                    letterSpacing: 0.5,
                    height: 1.5,
                    fontFamily: 'Opensans',
                    fontSize: 16.0,
                    fontWeight: FontWeight.normal),
              ),
            ),
            SizedBox(
              height: 40.0,
            ),
            ButtonTheme(
              //TODO:// make button #1 work!
              minWidth: 300.0,
              child: RaisedButton(
                child: Text('CONTACT ME'),
                padding: EdgeInsets.all(10.0),
                textColor: Colors.white,
                color: Color(0XFFeb1555),
                onPressed: _launchURL,
              ),
            ),
            SizedBox(
              height: 40.0,
            ),
            ButtonTheme(
              //TODO:// make button #2 work!
              minWidth: 300.0,
              child: RaisedButton(
                child: Text('FREQUENTLY ASKED QUESTIONS'),
                padding: EdgeInsets.all(10.0),
                textColor: Colors.black,
                color: Colors.white,
                onPressed: () {
                  print('second button tapped');
                },
              ),
            ),
            SizedBox(
              height: 150.0,
            ),
            FlatButton(
              child: Text(
                'Terms of Service & Privacy Policy →',
                style: TextStyle(
                  color: Color(0XFFeb1555),
                ),
              ),
              onPressed: () {
                print('privacy policy tapped');
              },
            ),
          ],
        ),
      ),
    );
  }
}

最佳答案

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

你在用模拟器吗?请记住,模拟器无法处理 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';
  }
}

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

关于flutter - 如何让 url 启动器发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62665015/

相关文章:

Flutter Web - 不支持的操作 : NaN error for canvas in Card widget

firebase - 如何在 Scoped Model 中从 Firestore 获取数据 - Flutter

flutter - 如何知道要导入哪个模块来使用一个类?

Flutter Spotify - 如何获得基于种子的推荐

listview - 在 Flutter 中,如何制作一个顶部和底部都是无限的 ListView ?

flutter - 如何在 Flutter 测试中找到 `FlatButton.icon`?

list - 对列表的多个子列表进行排序以创建新列表,Dart

dart - 在Dart中获取通用列表的子类型

flutter - flutter 中的 Webview 无法正常工作,出现平台错误

flutter - 如何在 flutter 中将 12 小时时间字符串转换为 24 小时?