この記事の想定読者
Flutterのwebのurl_launcher
パッケージを使用してメーラーソフトを起動する
空白文字が「+」として扱われてしまっていて困っている
イメージ画像
パッケージのインポート
$ flutter pub add url_launcher
- yaml
dependencies:
url_launcher:
コード
良い例
final bodyStr = '''
メール本文です。
スペース があっても+に置き換えられません。
''';
final email = 'John.Doe@example.com';
final title = Uri.encodeComponent('title');
final body = Uri.encodeComponent(bodyStr);
final mailtoUri = Uri.parse('mailto:$email?subject=$title&body=$body');
if (await canLaunchUrl(mailtoUri)) {
launchUrl(mailtoUri);
} else {
throw "Cloud not launch $mailtoUri.";
}
悪い例
final title = 'title';
final bodyStr = '''
メール本文です。
スペースがある場合+に置き換えられてしまいます。
''';
final uri = Uri(
scheme: 'mailto',
path: 'John.Doe@example.com',
queryParameters: {'subject': $title,'body':$bodyStr}
);
if (await canLaunch(scheme)) {
await launch(scheme);
} else {
print('problem on launcher');
}
参考