whatsapp_unilink

Dart package helping your app interact with WhatsApp via HTTP links (universal links). Works with Flutter.

The whatsapp_unilink package helps you build HTTP links and provides you with an idiomatic Dart class that:

  • converts your phone number into something that WhatsApp expects ✅
  • hides the encoding and link building details from you so that you can focus on your app 🚀
  • is blazingly fast ⚡️

Continuous Integration codecov whatsapp_unilink Published by dartside.dev GitHub Stars Count

If you enjoy using this package, a thumbs-up on pub.dev would be highly appreciated! 👍💙 Let's get to 500 likes 🚀

Usage

With WhatsAppUnilink, you can create a link that will allow your users to start a chat with someone (identified with phoneNumber).

By clicking the link, a chat with the person automatically opens. WhatsApp will include your message in text and it will automatically appear in the text field of a chat.

Both the phoneNumber and text arguments are optional. The WhatsAppUnilink instance, whenever converted to a string, will create a WhatsApp link for you.

When WhatsAppUnilink instance is converted to a string, any brackets, dashes, plus signs, and leading zeros or any other non-digit characters will be removed from the phone number.

The APIs support working with strings (toString()) and URIs (asUri).

Important: the whatsapp_unilink package does not convert from the local phone numbers format to the international format based on the device's locale.

Flutter

You may want to launch the WhatsApp app on your user's phone and make life easier for your users by pre-filling the mobile number and text.

Use the url_launcher for launching the links you create with the whatsapp_unilink package.

import 'package:whatsapp_unilink/whatsapp_unilink.dart';

// For Flutter applications, you'll most likely want to use
// the url_launcher package.
import 'package:url_launcher/url_launcher.dart';

// ...somewhere in your Flutter app...
launchWhatsAppUri() async {
  final link = WhatsAppUnilink(
    phoneNumber: '+001-(555)1234567',
    text: "Hey! I'm inquiring about the apartment listing",
  );
  // Convert the WhatsAppUnilink instance to a Uri.
  // The "launch" method is part of "url_launcher".
  await launch(link.asUri());
}

Alternatively, you can get the String representation of the links. You can either explicitly call the link.toString() method, or you may rely on Dart's shorter string interpolation, '$link'.

import 'package:url_launcher/url_launcher_string.dart';

launchWhatsAppString() async {
  final link = WhatsAppUnilink(
    phoneNumber: '+001-(555)1234567',
    text: "Hey! I'm inquiring about the apartment listing",
  );
  // Convert the WhatsAppUnilink instance to a string.
  // Use either Dart's string interpolation or the toString() method.
  // The "launchUrlString" method is part of "url_launcher_string".
  await launchUrlString('$link'); 
}

Dart

This package works on all Dart projects and doesn't have any Flutter-specific dependency. It works on the frontend, rendered to an a tag from your HTTP server, or returned from your Dart REST APIs.

You can convert your WhatsAppUnilink instances to String or Uri.

final link = WhatsAppUnilink(
  phoneNumber: '+001-(555)1234567',
  text: "Hey! I'm inquiring about the apartment listing",
);

final linkAsString = link.toString(); // or '$link'

final linkAsUri = link.asUri();

Libraries

The whatsapp_unilink library helps you create WhatsApp URLs.