Flutter Open WhatsApp & send Text Message to Specific WhatsApp

Kamal Bunkar
2 min readJul 11, 2021

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"),
),
),

),
);
}

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")));

}


}

}

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

--

--

Kamal Bunkar

Full Stack Developer #YouTuber #CourseCreator Passionate #Flutter #Android Developer. #MTech (CSE) #VNIT Nagpur. Let’s Talk for more details. #Dricoding.com