How to send whatsapp message using flutter?

Viewed 782

I am developing a small app with flutter and I want to send a WhatsApp message using flutter.

I have done with the code and it is working well, but what I want to do is to send the message automatically with out pressing send button in WhatsApp.

this is my code:

floatingActionButton: FloatingActionButton(
          onPressed: () {
            if (_formKey.currentState!.validate()) {
              sendMessage(context);
            }
          },
          tooltip: 'Send',
          child: const Icon(Icons.send),
        ),

This is how I send WhatsApp message:

final snackbar = const SnackBar(
    content: Text("Enter valid mobile number"),
    backgroundColor: Colors.red,
  );

  void sendMessage(context) {
    var phone = _phoneNumber.text;
    var message = _message.text;
    if (phone.length < 9) {
      ScaffoldMessenger.of(context).showSnackBar(snackbar);
    } else {
      _launchURL(phone, message);
    }
  }

  void _launchURL(phone, message) async => await canLaunch(
          'https://api.whatsapp.com/send?phone=967$phone&text=$message')
      ? await launch(
          'https://api.whatsapp.com/send?phone=967$phone&text=$message')
      : throw 'Could not launch WhatsApp';

This code will open WhatsApp and I have to press the send button to send the message, but I want to open WhatsApp and send the message automatically.

0 Answers
Related