Home > Blockchain >  Error opening Apps like WhatsApp to share text: Flutter
Error opening Apps like WhatsApp to share text: Flutter

Time:08-18

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- Error screen when button is pressed

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!

CodePudding user response:

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.

CodePudding user response:

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

  • Related