I want to launch WhatsApp application from my flutter application

9,388

Solution 1

You are missing the https:// in your url.

Replace the code below with your url() method:

  String url() {
    if (Platform.isAndroid) {
      // add the [https]
      return "https://wa.me/$phone/?text=${Uri.parse(message)}"; // new line
    } else {
      // add the [https]
      return "https://api.whatsapp.com/send?phone=$phone=${Uri.parse(message)}"; // new line
    }
  }

Solution 2

I found a solution adding this code to android/app/src/main/AndroidManifest.xml

<queries>
<intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
</intent>
<intent>
    <action android:name="android.intent.action.DIAL" />
    <data android:scheme="tel" />
</intent>
<intent>
    <action android:name="android.intent.action.SEND" />
    <data android:mimeType="*/*" />
</intent>

Solution 3

 String url() {
      if (Platform.isIOS) {
        return "whatsapp://wa.me/$phone/?text=${Uri.encodeFull(message)}";
      } else {
        return "whatsapp://send?phone=$phone&text=${Uri.encodeFull(message)}";
      }
    }
Share:
9,388
Moiz Khalil
Author by

Moiz Khalil

Updated on December 23, 2022

Comments

  • Moiz Khalil
    Moiz Khalil over 1 year

    I am using this dependency url_launcher: ^5.4.1 in my project to launch whatsapp through my flutter application but when i am pressing button to launch application it is not working but showing an error message on emulator that could not open the link. given below is the code with function i am using to launch whatsapp.

    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    import 'dart:io';
    
    
    void main() => runApp(Wapp());
    
    class Wapp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          theme: ThemeData(
              primarySwatch: Colors.orange,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
    final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
     void launchWhatsApp(
        {@required int phone,
        @required String message,
        }) async {
      String url() {
        if (Platform.isAndroid) {
          return "whatsapp://wa.me/$phone:03452121308:/?text=${Uri.parse(message)}";
        } else {
          return "whatsapp://send?   phone=$phone&text=${Uri.parse(message)}";
        }
      }
    
      if (await canLaunch(url())) {
        await launch(url());
      } else {
        throw 'Could not launch ${url()}';
      }
    }
    
    
    Widget build(BuildContext context){
      return Scaffold(
        appBar: AppBar(
          title: Text("Home"), 
        ),
    
      body: Center(
        child: RaisedButton(
          
          color: Colors.orange,
          textColor: Colors.black,
          padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 60.0),
          highlightColor: Colors.green,
          onPressed: () {
            launchWhatsApp(phone: 03452121308, message: 'Hello');
          },
          child: Text("Place Your Order",style: TextStyle(
              
              fontWeight: FontWeight.bold,
              fontSize: 15
              
            )
          )
        )
      )
    
      );
    }
    
    }
    
  • Moiz Khalil
    Moiz Khalil over 3 years
    Still not working after adding https it is showing site can't be reach. Moreover i want to launch application not website
  • void
    void over 3 years
    If you are trying to send a message, consider using: the url https://wa.me/$phone/?text=${Uri.parse(message)}. You provided a wrong url. I updated my answer to use the correct url.
  • Moiz Khalil
    Moiz Khalil over 3 years
    yes it is now working but how will i open chat with specifi contact
  • void
    void over 3 years
    The code above opens a specific contact as long as you give it a correct phone. @MoizKhalil. I have no idea what else you are asking for.
  • 7uc1f3r
    7uc1f3r over 3 years
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
  • Tuhin
    Tuhin over 2 years
    @void I tried your code..its works with all iPhone devices also some low-end Andriod devices..But for all high-spec Andriod devices WhatsApp is not opening..(not showing error.. not even clickable... )Do I have to make some changes in the Andriod.xml file?