简体   繁体   中英

Error opening Apps like WhatsApp to share text: Flutter

I am trying to open apps like WhatsApp, Twitter and Facebook from inside my flutter app but I get a URL scheme error. Please help! The code I am using is

                      child: ElevatedButton(
                        onPressed: () async {
                          String url =
                              "https://api.whatsapp.com/send?text=Hello there!";
                          var encoded = Uri.encodeFull(url);
                          Uri whatsAppUri = Uri.parse(encoded);
                          if (await canLaunchUrl(whatsAppUri)) {
                            await launchUrl(whatsAppUri);
                          }
                        },

I get the following error screen- 按下按钮时的错误屏幕

Please help me find the correct method and URL. Also please help me with Twitter and Facebook as I need to use them as well. Thank You!

Use this:

const url = "whatsapp://send?text=Hello World!"

or

const url = "https://wa.me/?text=Hello World!";

and add this to your info.plist if your using ios:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>whatsapp</string>
</array>

for facebook:

const url = "fb://facewebmodal/f?href=$username";

for twitter:

const url = "https://twitter.com/username";

last but not least make sure you add configuration right, you can find more information from here

You can use below code:

String url() {
    if (Platform.isAndroid) {
      return "https://wa.me/$phone/?text=Hello there!";
    } else {
      return "https://api.whatsapp.com/send?phone=$phone=Hello there!}";
    }
  }

String url = url();
var encoded = Uri.encodeFull(url);
                          Uri whatsAppUri = Uri.parse(encoded);
                          if (await canLaunchUrl(whatsAppUri)) {
                            await launchUrl(whatsAppUri);
                          }

In the place of $phone you can write phone number of user to which you want to send.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM