DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Flutter Open WhatsApp & send Text Message to Specific WhatsApp
kamal bunkar
kamal bunkar

Posted on

Flutter Open WhatsApp & send Text Message to Specific WhatsApp

In Flutter Open Whatsapp and send text message is very easy. You can do it without using Flutter Share Plugin. If You want to launch Whatsapp application from Flutter code with predefine text message then you should follow this tutorial How Flutter Open WhatsApp And Send Text Message. You can dynamically change the WhatsApp number as well. Just get the working WhatsApp number from server api call and use it on flutter code given below as dynamic variable.
This Flutter Code is working on Both Android & iPhone. So I will suggest you to run this code on Real Mobile phone not on emulator. WhatsApp Must installed on your mobile phone. Otherwise you will get an error β€œwhatsapp is not installed”.
Create a button
I am going to Create a simple text button in Stateful widget and on Button Click whatsapp app will be open. If you have confusion about stateless and stateful widget, so Please read my article. To make text as clickable widget, wrap Text widget with a GestureDetection Widget. Once use will click on button, we will call a function name as β€œopenwhatsapp()β€œ. You can choose other name as well. Below is the code for build method. You can follow this tutorial How Flutter Open WhatsApp And Send Text Message for complete code.


@override
Widget build(BuildContext context) {
  return Scaffold(
    body: SafeArea(
      child: GestureDetector(
        onTap: (){
          openwhatsapp();
        },
        child: Container(
          padding: EdgeInsets.all(40),
          child: Text(" contact US"),
        ),
      ),

    ),
  );
}

Enter fullscreen mode Exit fullscreen mode

Create Function
Now we need to write code in openwhatsapp function. We need to write separate code for android and iphone. The only difference is on URL string only , rest of the code is same. Below is the code for open whatsapp and send text predefine text message.


openwhatsapp() async{
  var whatsapp ="+919144040888";
  var whatsappURl_android = "whatsapp://send?phone="+whatsapp+"&text=hello";
  var whatappURL_ios ="https://wa.me/$whatsapp?text=${Uri.parse("hello")}";
  if(Platform.isIOS){
    // for iOS phone only
    if( await canLaunch(whatappURL_ios)){
       await launch(whatappURL_ios, forceSafariVC: false);
    }else{
      ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: new Text("whatsapp no installed")));

    }

  }else{
    // android , web
    if( await canLaunch(whatsappURl_android)){
      await launch(whatsappURl_android);
    }else{
      ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: new Text("whatsapp no installed")));

    }


  }

}

Enter fullscreen mode Exit fullscreen mode

Now run the code on real mobile phone. How Flutter Open WhatsApp And Send Text Message.
If you have any questions please feel free to contact me at www.dripcoding.com

Top comments (3)

Collapse
 
abodisadiq6 profile image
Abodi Sadiq • Edited on

This fills up the field, but does not press the Send key. Is there a way to automate the whole process including pressing the Send key.

Second, How can I send to WhatsApp group no to number?

All thanks in advanced.

Collapse
 
gleisser profile image
Gleisser

nice, simple and straight to the point

Collapse
 
rx40 profile image
Petrus-Nauyoma

Did it work perfectly for you?

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.