how-to-enable-smart-link-sharing-deep-linking-in-flutter-for-whatsapp-urls

How to Enable Smart Link Sharing & Deep Linking in Flutter for WhatsApp URLs?

Deep linking is a powerful feature in mobile apps that allows users to open specific content within an app directly via a URL. It enhances user experience by reducing the steps needed to reach desired content and supports seamless platform transitions.

In this guide, you’ll learn how to:

  • Generate a deep link dynamically in Flutter
  • Share it via WhatsApp
  • Handle incoming deep links to navigate users to specific pages in your app

Let’s walk through the complete process to get your Flutter app deep-link ready!

Setting Up Deep Linking in Flutter

To enable deep linking in a Flutter app, you must configure Android and iOS platforms appropriately. Here, we’ll focus on the Android setup first.

Step 1: Modify AndroidManifest.xml

In android/app/src/main/AndroidManifest.xml, add an intent filter inside <activity>:

				
					<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="http" android:host="yourdomain.com" android:pathPrefix="/shop"/>
</intent-filter>

				
			

Replace yourdomain.com with your app’s actual domain or custom scheme.

Step 2: Install Deep Link Package

Add uni_links package in pubspec.yaml:

				
					dependencies:
  uni_links: ^0.5.1

				
			

Run flutter pub get to install dependencies.

This package helps handle incoming deep links within your Flutter app.

Generating a Shareable Product URL

Use a simple string generator function to create a link that navigates users to a specific product detail page.

				
					String generateProductLink(String productName,  int productId) {
   String formattedName = productName.replaceAll(‘ ‘, ‘-’);
  return 'https://yourdomain.com/shop/$formattedName-$productId';
}

				
			

You can call this function wherever you need to generate a dynamic link for a particular product or content ID.

Sharing the URL via WhatsApp

Sharing links across messaging platforms like WhatsApp is a great way to promote in-app content. Here’s how to integrate it.

1. Install url_launcher package:

Add the following to your pubspec.yaml:

				
					dependencies:
  uni_links: ^0.5.1

				
			

2. Implement the sharing function:

				
					import 'package:url_launcher/url_launcher.dart';
void shareOnWhatsApp(String productName, int productId, String price) {
  String link = generateProductLink(productId, productName, price);
  String message = "Check out this product: $productName \n Price: $price \n 🔗 $link";
  String whatsappUrl = "https://wa.me/?text=${Uri.encodeComponent(message)}";
  launchUrl(Uri.parse(whatsappUrl), mode: LaunchMode.externalApplication);
}

				
			

This function opens WhatsApp with the deep link pre-filled in the message box.

Handling Deep Links in Flutter

When a user clicks on a deep link, your app must listen to that event and respond accordingly. Here’s how to handle it:

Step 1: Initialize Deep Link Listener

Inside initState(), set up deep link handling:

				
					import 'package:uni_links/uni_links.dart';
void _handleDeepLinks() {
  getInitialUri().then((uri) {
    if (uri != null) {
      _navigateToScreen(uri);
    }
  });
  uriLinkStream.listen((uri) {
    if (uri != null) {
      _navigateToScreen(uri);
    }
  });
}

				
			

Ensure you have set up the named route /productDetail in your app’s navigation.

Step 2: Navigate to Product Detail Page

On your ProductDetailPage, receive and use the argument as follows:

				
					void _navigateToScreen(Uri uri) {
  List segments = uri.pathSegments;
  if (segments.isNotEmpty &amp;&amp; segments.first == 'shop') {
    String productId = segments.last.split('-').last;
    Get.toNamed('/product_detail', arguments: [productId]);
  }
}

				
			

This ensures users land directly on the product screen via the link.

Testing and Debugging

Testing is key to ensuring a smooth deep-linking experience. Here’s how to do it:

 1. Run a Local Test:

Open a browser and enter:

				
					http://yourdomain.com/pathPrefix/screenname
				
			
2. Use ADB to Test Deep Links:
				
					Adb shell am start  -a android.intent.action.VIEW  -d “heep:
//yourdomain.com/pathprefix/sreenname

				
			
3. Test WhatsApp Sharing:

Run the app and trigger shareOnWhatsApp().

Flutter's deep linking and smart sharing bring your app to life!

With deep linking and smart sharing in Flutter, you can:

  • Guide users to specific content
  • Share dynamic links via platforms like WhatsApp
  • Create a smoother, more engaging app experience

This functionality enhances marketing efforts, boosts user engagement, and makes your app more versatile across platforms.

Ready to make your Flutter app smarter? Implement these steps and watch how effortlessly your content connects with users!

Picture of Sushma Akula

Sushma Akula

I'm Sushma Akula, a Flutter Junior Developer passionate about building seamless, cross-platform mobile apps with intuitive UI/UX. I love solving real-world problems and exploring the dynamic world of mobile and open-source tech

Get a free quote