Home > Back-end >  problem with launching whatsapp to send a message with flutter url_launcher
problem with launching whatsapp to send a message with flutter url_launcher

Time:08-08

this is the function to send an WhatsApp message (or just launch the WhatsApp with the message)

in the Cipher.dart

void sendCodeByWhatsApp(
       String phone,
      String message,
      ) async {
    String url() {
      if (Platform.isAndroid) {
        return "https://wa.me/$phone/?text=$message";
      } else {
        return "https://api.whatsapp.com/send?phone=$phone=$message";
      }
    }
    if (await canLaunchUrl(Uri.parse(url()))) {
      await launchUrl(Uri.parse(url()));
    } else {
      throw 'Could not launch ${url()}';
    }
  }

and here I use it:

 ElevatedButton(
                         child: const Icon(Icons.whatsapp, color: Colors.white,),
                         onPressed: (){
                             Cipher().sendCodeByWhatsApp(encrypt.encrypt, phone.text);
                         },
                       ),

when adding a number and message, just open a page with WhatsApp logo, tells me:

we couldn't find the page you were looking for

CodePudding user response:

Refer to this to get how to use the link properly link

There may also be an error if the number includes the country code like

1 0123456789

Will give an error. The phone should not include the country code.

CodePudding user response:

after doing some search, finally found the solution:

same code above, but the URL should be like this:

 "whatsapp://send?phone=$phone&text=${Uri.parse(message)}";

for android, and it is working like a charm...

  • Related