在Flutter中使用URL启动程序发送电子邮件时出现问题

kknvjkwl  于 2022-12-14  发布在  Flutter
关注(0)|答案(9)|浏览(272)

你好,我试着启动电子邮件页面与recipent。我试过Flutter电子邮件发送者谁与Android工作,但不是在iOS为我。所以我尝试了网址启动器做同样的事情,但不工作也与iOS。我使用iOS模拟器,问题可以是这个?
我使用这个url启动器的例子

mailto:xxxxx@xxxxx.com?subject=News&body=New%20plugin

我有此错误

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: Could not launch mailto:xxxx@xxxx.com?subject=News&body=New%20plugin
#0      _menuscreenState._launchURL (package:xxxx/bottom.dart:8285:7)
<asynchronous suspension>
#1      _menuscreenState.build.<anonymous closure>.<anonymous closure> (package:xxxx/bottom.dart:8705:13)

下面是url启动器的完整示例

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  String email="contact@ouiquit.com";
  _launchEmail() async {
    if (await canLaunch("mailto:$email")) {
      await launch("mailto:$email");
    } else {
      throw 'Could not launch';
    }
  }

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.red),
      home: Scaffold(
        appBar: AppBar(
          title: Text('test mail'),
          actions: <Widget>[
            IconButton(
              onPressed: _launchEmail,
              icon: Icon(Icons.send),
            )
          ],
        ),

      ),
    );
  }

}

此处为错误

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: MissingPluginException(No implementation found for method canLaunch on channel plugins.flutter.io/url_launcher)
#0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:7)
<asynchronous suspension>
#1      canLaunch (package:url_launcher/url_launcher.dart:112:25)
<asynchronous suspension>
#2      _MyAppState._launchEmail (package:testmail/main.dart:20:15)
<asynchronous suspension>
#3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
#4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
#6      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
#7      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
#8      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/<…>
agyaoht7

agyaoht71#

这对我在Android和iOS设备上都适用:
如果您也有subjectbody,请添加query参数。

final Uri params = Uri(
  scheme: 'mailto',
  path: 'email@example.com',
  query: 'subject=App Feedback&body=App Version 3.23', //add subject and body here
);

var url = params.toString();
if (await canLaunch(url)) {
  await launch(url);
} else {
  throw 'Could not launch $url';
}
sirbozc5

sirbozc52#

以上给出的解决方案将适用于API < 30,但对于API >= 30,需要将以下内容添加到您的AndroidManifest.xml文件中,以便电子邮件发送工作。

<queries>
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="https" />
    </intent>
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="*/*" />
    </intent>
</queries>

source

dwbf0jvd

dwbf0jvd3#

这是我使用url_launcher发送邮件时使用的函数:

void _launchURL() async {
    final Uri params = Uri(
      scheme: 'mailto',
      path: 'my.mail@example.com',
    );
    String  url = params.toString();
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print( 'Could not launch $url');
    }
  }
44u64gxh

44u64gxh4#

适用于iOS
iOS模拟器上没有安装电子邮件应用程序,您只能在真实的设备上测试。

jk9hmnmh

jk9hmnmh5#

对于所有仍然想知道为什么它不像我那样工作的人。记住模拟器不能处理mailto,因为邮件应用程序不可用。如果你在调用launch之前使用canLaunch,你会得到更多的信息。

rwqw0loc

rwqw0loc6#

尝试mailto插件
https://pub.dev/packages/mailto#-readme-tab-
它在设备上的Android和iOS都能工作,工作得很好。

void funcOpenMailComposer() async{

    final mailtoLink = Mailto(
      to: ['test@gmail.com'],
      cc: ['test12@gmail.com','test13@gmail.com'],
      subject: '',
      body: '',
    );
  await launch('$mailtoLink');
}
uxhixvfz

uxhixvfz7#

请试试这个,我用过这个,它对我来说很好用。

_launchEmail() async {
      if (await canLaunch("mailto:$email")) {
        await launch("mailto:$email");
      } else {
        throw 'Could not launch';
      }
    }
axkjgtzd

axkjgtzd8#

对于iOS,我有这样添加

**

_launchEmail() async {
    // ios specification
    final String subject = "Subject:";
    final String stringText = "Same Message:";
    String uri = 'mailto:administrator@gmail.com?subject=${Uri.encodeComponent(subject)}&body=${Uri.encodeComponent(stringText)}';
    if (await canLaunch(uri)) {
      await launch(uri);
    } else {
      print("No email client found");
    }
  }

**

72qzrwbm

72qzrwbm9#

我在我的清单中添加了这一行,我能够开始一个呼叫,打开Map等。

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

more info

相关问题