Why is Dart Uri replacing space with "+"?

Viewed 1855

I am using the Urilauncher plugin for flutter to open the mail client. This is my code,

final Uri _emailLaunchUri = Uri(
                  scheme: 'mailto',
                  path: 'info@moops.in',
                  queryParameters: {'subject': 'Feedback for Moops'});

await launch(_emailLaunchUri.toString());

However, In the email client. The space in the subject gets substituted by "+" symbol. I already tried replacing spaces with %20 and   but then the space gets substituted with these characters. Is there any other special character that I should use to avoid the + symbol and have the spaces instead. Or maybe a way to escape the space.

2 Answers

I found a solution to bypass this issue.

    Uri _emailLaunchUri = Uri(
         scheme: 'mailto',
         path: 'example@example.com',
         queryParameters: {
         'subject': 'Example Subject & Symbols are allowed!'
         }
    });
              
    launch(
       _emailLaunchUri.toString().replaceAll("+", "%20")
    );

The replace all function replaces all + symbols with %20 which stand for space and this solves the problem perfectly.

You could use the string literals to get past this, I have written a small function bellow to take care of this:

String getUrl(String scheme, String path, Map<String, String> queryParameters) {
  String url = '$scheme:$path?';

  queryParameters.forEach((String k, String v){
    url += '$k=$v&';
  });

  return url;
}

Then you could call it like so:

await launch(getUrl('mailto', 'info@moops.in', {'subject': 'Feedback for Moops'}));
Related