What is a dynamic link?
A Dynamic-link is a smart link that provides the best user experience on your native app such that you can control the specific location of the application to where the user should be redirected to. By using dynamic links, if the user does not install the specific app on their mobile phone and if the user opens the link through a mobile phone you can redirect the user to the Android Play Store or Apple Store by forcing to install the specific app to their mobile phone. And if not an app is already on the user’s mobile phone then the user will redirect inside the app-specific location.
In this article, the usage of the dynamic link will implement using Flutter and Firebase.
In this case, we will provide a solution with a programmatically easiest way such as user redirects to a specific page when the app is installed to their mobile phone. To make clear deep link creation and its purpose, there is a dummy project with three pages the home page, login page, and profile page. The home page is the initial page and if the app is trying to open with a deep link it will redirect to the login page.
You need to have an existing flutter project or create a new flutter project using flutter create <Your Project Name>
command.
Then there is a plugin called “firebase_dynamic_links” and add it to your created project’s pubspec.yaml file.
This plugin will help to detect firebase dynamic links in our application.
firebase_dynamic_links: ^2.0.6
Let’s go with following steps………..
First setup project in firebase console. To setup , see my articale Google Sign-In & Firebase Authentication Using Flutter.
After successfully adding google-services.json and google-services.plist file to your app folder, you need to add Firebase SDK setup in native apps.
Now we have successfully initial configuration and adding Firebase to our existing project. Then we will move on to the next step.
Setting up Firebase
On the right side of the Firebase console, select “Dynamic Links” after redirecting to your firebase flutter dynamic link project. Then click “Get started”.

Selecting a domain under the given modal “Add URL prefix”. Here they will provide automatically some domains. If you need to customize it you can enter the domain you preferred. Then click “Continue” button.

Create a dynamic link inside the Firebase Console
Click “New Dynamic Link”.

Set up a short URL link and click “Next”.

Then you need to set up your dynamic link URL, under “Set up your Dynamic Link”. There is a deep link to your application and if a specific user has not installed the app where that user should redirect. As an example, you can provide app play store link as the dynamic link. Link name can be given as any meaningful short name about your dynamic link that you prefer. Click “Next”.

Choose “Open the deep link URL in a browser” option. In that option, if the specific app is not installed in your app the link will open through the browser. If not you can choose “Open the deep link in your iOS App” but if so you need to have an iOS app. Then click “Next”.

Here we define behavior for Android. Select “Open the deep link in your Android App” and choose your android app. Then click “Next”.

Additionally, you can customize some advanced options. Then click “Create”.

Under the URL, you will get your created dynamic link.

AndroidManifest.xml Configuration
Add your deep link domains to your android/app/src/main/AndroidManifest.xml
so your app can receive the Dynamic Link data after it is installed/updated from the Play Store. Refer to the official docs to illustrate setup.
For example:
<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:host="example.com"
android:scheme="https"/>
</intent-filter>
Handling dynamic links inside the app
5.1 Here we need to install “get package” to handle the navigation among the pages. Therefore add that package to your created project’s pubspec.yaml file.
get: ^4.1.4
Let’s implement the following method in the main.dart file. This method will detect firebase dynamic link and navigate to specific page, in this case LogInPage().
void initDynamicLinks() async {
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
final Uri deepLink = dynamicLink?.link;
print("deeplink found");
if (deepLink != null) {
print(deepLink);
Get.to(() => LogInPage(title: 'firebase_dynamic_link navigation'));
}
}, onError: (OnLinkErrorException e) async {
print("deeplink error");
print(e.message);
});
}
After implementing above method complete code of the main.dart file like below.
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:dynamic_links/homepage.dart';
import 'package:dynamic_links/loginpage.dart';
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
initState() {
super.initState();
this.initDynamicLinks();
}
void initDynamicLinks() async {
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
final Uri deepLink = dynamicLink?.link;
print("deeplink found");
if (deepLink != null) {
print(deepLink);
Get.to(() => LogInPage(title: 'firebase_dynamic_link navigation'));
}
}, onError: (OnLinkErrorException e) async {
print("deeplink error");
print(e.message);
});
}
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: HomePage(title: 'Demo Home Page'),
);
}
}
Now you will end up with successful dynamic link creation using Flutter and Firebase. In this case, if your app opens through the mobile phone it will normally redirect to the home page. If not app is installed and tries to open it using the dynamic link, it will redirect to the login page. At last, if the app is not installed and tries to open the dynamic link, then the user will redirect to the deep link URL that you provide when creating the dynamic link.
Through this GitHub repository, you can get further code implementation.
Thank you for reading!!!