WhatsApp Launch on Flutter Code

Hasnain Mirrani
1 min readDec 13, 2023

--

To launch WhatsApp through the url_launcher package in Flutter, you can use the launch method to open a specific URL. For WhatsApp, you can use a whatsapp:// URL scheme with the phone number or a chat link.

Here’s an example:

Add the url_launcher dependency to your pubspec.yaml file:

dependencies:
url_launcher: ^6.0.6

Make sure to run flutter pub get after adding the dependency to fetch the package.

Use the url_launcher package to launch WhatsApp. Here's an example function that opens WhatsApp:

call this function to any button and launch whatsapp

whatsapp() async {
String contact = "92*********";
String text = '';
String androidUrl = "whatsapp://send?phone=$contact&text=$text";
String iosUrl = "https://wa.me/$contact?text=${Uri.parse(text)}";

String webUrl = 'https://api.whatsapp.com/send/?phone=$contact&text=hi';

try {
if (Platform.isIOS) {
if (await canLaunchUrl(Uri.parse(iosUrl))) {
await launchUrl(Uri.parse(iosUrl));
}
} else {
if (await canLaunchUrl(Uri.parse(androidUrl))) {
await launchUrl(Uri.parse(androidUrl));
}
}
} catch(e) {
print('object');
await launchUrl(Uri.parse(webUrl), mode: LaunchMode.externalApplication);
}
}

n this example, the launchWhatsApp function creates a WhatsApp URL using the wa.me domain. Replace '123456789' with the actual phone number you want to open in WhatsApp. The canLaunch function checks if the URL can be launched, and if so, the launch function opens the URL.

Remember to handle exceptions appropriately in your production code and consider user experience when implementing such features.

--

--

Hasnain Mirrani

Update the lattest and well explain All about Flutter make you from Zero to Hero in Flutter. follow https://www.linkedin.com/in/hasnain-mirrani-b47ab7131