Dynamic links are a powerful tool for mobile app developers to drive user engagement and retention. With dynamic links, you can create links that deep-link directly to specific content within your app, even if the user doesn’t have the app installed yet. In this article, we’ll show you how to set up dynamic links in Flutter.
Step 1: Set up Firebase
The first step in setting up dynamic links in Flutter is to set up Firebase. Firebase is a mobile and web application development platform that provides a variety of tools and services to help you build high-quality apps. To set up Firebase, follow these steps:
- Go to the Firebase Console and sign in with your Google account.
- Click on “Add project” and enter a name for your project.
- Follow the prompts to set up your project.
Once you’ve set up your Firebase project, you’ll need to add the Firebase SDK to your Flutter project.
Step 2: Add the Firebase SDK to your Flutter project
To add the Firebase SDK to your Flutter project, follow these steps:
- Open your Flutter project in Android Studio or Visual Studio Code.
- Open the pubspec.yaml file and add the following dependencies:
dependencies:
firebase_core: ^1.0.0
firebase_dynamic_links: ^2.0.0
- Run
flutter pub get
to install the dependencies.
Step 3: Create a dynamic link
To create a dynamic link, follow these steps:
- Open the Firebase Console and select your project.
- Click on “Dynamic Links” in the left-hand menu.
- Click on “Get Started” and follow the prompts to create a new dynamic link.
When creating a dynamic link, you’ll need to specify the following:
- The link URL: This is the URL that the user will be directed to when they click on the dynamic link.
- The iOS and Android package names: These are the package names for your app on iOS and Android.
- The link behavior: This determines what happens when the user clicks on the dynamic link. You can choose to open the link in your app, on the web, or in another app.
Step 4: Handle the dynamic link in your Flutter app
To handle the dynamic link in your Flutter app, follow these steps:
- Add the following code to your main.dart file:
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
handleDynamicLinks();
runApp(MyApp());
}
void handleDynamicLinks() async {
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
final Uri deepLink = dynamicLink?.link;
// Handle the deep link here.
},
onError: (OnLinkErrorException e) async {
print('onLinkError');
print(e.message);
}
);
final PendingDynamicLinkData data = await FirebaseDynamicLinks.instance.getInitialLink();
final Uri deepLink = data?.link;
// Handle the deep link here.
}
- In the
onSuccess
callback, you can handle the deep link by extracting the relevant data from theUri
object. - In the
onError
callback, you can handle any errors that occur when processing the dynamic link. - In the
getInitialLink
method, you can retrieve the initial dynamic link that launched the app.
And that’s it! With these steps, you can set up dynamic links in your Flutter app and drive user engagement and retention.