mailto: link for Flutter for Web

3,558

After experimenting a little I found a way to make url_launcher work with web.

Don't use canLaunch(url). Instead, just use launch(url), but wrap it in try-catch block. This way you should be safe and the email link will work. For catch you can just copy the email to clipboard and notify the user about that with a snackbar or smth. Probably not the best solution, but a good one, till we get something better.

Here is the sample code, so that you see what I mean:

void _launchMailClient() async {
  const mailUrl = 'mailto:$kEmail';
  try {
    await launch(mailUrl);
  } catch (e) {
    await Clipboard.write(kEmail);
    _emailCopiedToClipboard = true;
  }
}
Share:
3,558
Jason O.
Author by

Jason O.

I'm loving Elm, Elixir, Phoenix, and Python.

Updated on December 15, 2022

Comments

  • Jason O.
    Jason O. over 1 year

    The url_launcher package (https://pub.dev/packages/url_launcher) doesn't seem to work for Flutter for Web. The following code prints "test url1" but nothing happens afterwards.

    How can I implement mailto: like functionality in Flutter for Web which causes the default email app to open with a prepopulated 'to:' email address?

    FlatButton(
      onPressed: _mailto, //() => {},
      padding: EdgeInsets.all(3.0),
      child: _contactBtn(viewportConstraints),
    )
    
    
      _mailto() async {
        const url = 'mailto:[email protected]?subject=Product Inquiry&body=';
        print("test url1");
        if (await canLaunch(url)) {
          print("test url2");
          await launch(url);
        } else {
          print("test url3");
          throw 'Could not launch $url';
        }
      }