我在 Flutter 中使用 url_launcher 插件与主题共享电子邮件,它在 Android 中使用此包运行良好,但在 Ios 或 Iphone 上没有任何反应。
我已经为相同的功能创建了以下功能:
void _launchEmail() async {
String email = Uri.encodeComponent("");
String productTitle =
_catalogStore.summaryInfo?.data?[widget.productIndex].title ?? "";
String productId =
_catalogStore.summaryInfo?.data?[widget.productIndex].id ?? "";
String subject = Uri.encodeComponent(
"mpo_product".tr() + " - " + productTitle + "($productId)");
String body = Uri.encodeComponent("");
Uri mail = Uri.parse("mailto:$email?subject=$subject&body=$body");
try {
await launchUrl(mail);
} catch (e) {
ToastUtil.show(
ToastDecorator(
isSuccess: false,
msg: "something_went_wrong".tr() + "\n" + e.toString()),
context,
gravity: ToastGravity.bottom);
}
}
可能是什么问题?
Uri 编码查询参数的方式存在错误 => Bug
使用以下函数对您的查询组件进行编码:
String? encodeQueryParameters(Map<String, String> params) {
return params.entries.map((MapEntry<String, String> e) =>'${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}').join('&');
}
......
final Uri emailLaunchUri = Uri(
scheme: 'mailto',
path: '[email protected]',
query: encodeQueryParameters(<String, String>{
'subject': 'Example Subject & Symbols are allowed!',
}),
);
launchUrl(emailLaunchUri);