Actually I use this code in Flutter:
onPressed: () async {
String appUrl;
String phone = '+989125272xxx'; // phone number to send the message to
String message = 'Merhaba,'; // message to send
if (Platform.isAndroid) {
appUrl = "whatsapp://send?phone=$phone&text=${Uri.parse(message)}"; // URL for Android devices
} else {
appUrl = "https://api.whatsapp.com/send?phone=$phone=${Uri.parse(message)}"; // URL for non-Android devices
}
// check if the URL can be launched
if (await canLaunchUrl(Uri.parse(appUrl))) {
// launch the URL
await launchUrl(Uri.parse(appUrl));
} else {
// throw an error if the URL cannot be launched
throw 'Could not launch $appUrl';
}
},
but it is very important, you need to add the QUERY_ALL_PACKAGES permission to work fine on Android 12 or higher devices to your app's AndroidManifest.xml file. This permission allows your app to query all installed packages on the device, including WhatsApp.
Add the following line inside the manifest tag in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />