Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem to send email with URL launcher in flutter

Tags:

flutter

Hello I try to launch email page with recipent. I tried flutter email sender who work with android but not on ios for me. So I tried url launcher to do the same thing, but not working also with iOS. I use iOS simulator, the problem can be this ?

I use this example of url launcher

mailto:[email protected]?subject=News&body=New%20plugin

I have this error

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: Could not launch mailto:[email protected]?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)

Here is the full example with url launcher

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="[email protected]";
  _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),
            )
          ],
        ),


      ),
    );
  }


}

Here is the error

[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/<…>
like image 349
Nitneuq Avatar asked Sep 18 '19 09:09

Nitneuq


3 Answers

The above given solutions will work for API < 30 but for API >= 30 the following is needed to be added to your AndroidManifest.xml file for email sending to work.

<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

like image 155
StuckInPhDNoMore Avatar answered Oct 23 '22 06:10

StuckInPhDNoMore


This works for me on both Android and iOS devices:

Add query parameter if you have subject and body too.

final Uri params = Uri(
  scheme: 'mailto',
  path: '[email protected]',
  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';
}
like image 31
Ravinder Kumar Avatar answered Oct 23 '22 04:10

Ravinder Kumar


This is the function I use to send mail using url_launcher :

void _launchURL() async {
    final Uri params = Uri(
      scheme: 'mailto',
      path: '[email protected]',
    );
    String  url = params.toString();
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print( 'Could not launch $url');
    }
  }
like image 3
Axel Frau Avatar answered Oct 23 '22 06:10

Axel Frau