url_launcher 不在 wp 中发送消息

问题描述 投票:0回答:1

我正在尝试使用 url_launcher 使用以下代码将我的行踪发送给我的应用程序中的紧急联系人,但它不起作用,有人可以帮助我吗

字符串位置消息 = '紧急情况!我需要帮助。这是我当前的位置:https://www.google.com/maps/search/?api=1&query=${position .纬度},${位置.经度}';

  String phoneNumber = '372990290'; // Replace with your emergency contact's number

  String whatsappUrl = "whatsapp://send?phone=$phoneNumber&text=$locationMessage";


  if (await url_launcher.canLaunchUrl(Uri.parse(whatsappUrl)) ){
    await url_launcher.launchUrl(Uri.parse(whatsappUrl));
  } else {
    throw 'Could not launch $whatsappUrl';
  }
}

}我希望它在我按下按钮后发送手机的位置,但它一直给我一个错误,说上述内容的组件名称为空,因此无法启动whatsappurl,我该如何解决它

flutter dart url launcher
1个回答
0
投票

您可以尝试以下功能在WhatsApp上发送消息

void _shareLocationThroughWhatsApp() async {
  String contact = "91*********";
  String text = 'Emergency! I need help. Here is my current location: https://www.google.com/maps/search/?api=1&query=${position.latitude},${position.longitude}';
  String androidUrl = "whatsapp://send?phone=$contact&text=$text";
  String iosUrl = "https://wa.me/$contact?text=${Uri.parse(text)}";

  String webUrl = 'https://api.whatsapp.com/send/?phone=$contact&text=hi';

  try {
    if (Platform.isIOS) {
      if (await canLaunchUrl(Uri.parse(iosUrl))) {
        await launchUrl(Uri.parse(iosUrl));
      }
    } else {
      if (await canLaunchUrl(Uri.parse(androidUrl))) {
        await launchUrl(Uri.parse(androidUrl));
      }
    }
  } catch(e) {
    print('object');
    await launchUrl(Uri.parse(webUrl), mode: LaunchMode.externalApplication);
  }
}

如果 url_launcher 无法自行启动外部应用程序,则必须在启动 URL 时添加 mode: LaunchMode.externalApplication

© www.soinside.com 2019 - 2024. All rights reserved.