Using firebase_dynamic_links Package in Flutter
What is Firebase Dynamic Links?
Firebase Dynamic Links are customizable and dynamically generated deep links for mobile applications. These deep links can directly redirect users to specific content and enhance the in-app experience. Firebase Dynamic Links make it easier for users to switch between applications and increase user engagement.
Step 1: Setting up the Project Structure To use Firebase Dynamic Links in a Flutter project, you first need to create a Firebase project. You can create a new project and complete the necessary configuration steps in the Firebase Console. Then, add the firebase_core and firebase_dynamic_links packages to integrate Firebase into the Flutter project.
Step 2: Adding the Firebase Dynamic Links Package To use the firebase_dynamic_links package in a Flutter project, add the following dependency to the pubspec.yaml file:
dependencies:
firebase_dynamic_links: ^5.0.0
After adding the dependency, you can update the Flutter project by running the command flutter pub get
in the terminal.
Step 3: Creating and Using Deep Links To create and use deep links with Firebase Dynamic Links, you can follow the steps below:
- Go to the “Dynamic Links” tab in the “Firebase Console” of your Firebase project.
- Create a new dynamic link and specify the target URL.
- Add any necessary parameters (optional).
- Generate the deep link and obtain the provided short URL.
- In the Flutter application, import the firebase_dynamic_links package:
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// Firebase Dynamic Links listener
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData? dynamicLink) async {
final Uri? deepLink = dynamicLink?.link;
if (deepLink != null) {
// Handle the deep link
// Perform the desired action
}
},
onError: (OnLinkErrorException e) async {
print('Dynamic Link Error: ${e.message}');
},
);
runApp(MyApp());
}
To handle the deep link, you can use the following code when a button is clicked, for example:
FirebaseDynamicLinks.instance.getInitialLink().then(
(PendingDynamicLinkData? dynamicLink) {
final Uri? deepLink = dynamicLink?.link;
if (deepLink != null) {
// Handle the deep link
// Perform the desired action
}
},
);
By following these steps, you can use the firebase_dynamic_links package in your Flutter application. With Firebase Dynamic Links, you can direct your users to specific content with customizable and dynamically generated deep links, enhancing the user experience.
In this article, we learned how to use the firebase_dynamic_links package in Flutter to create and use deep links. Firebase Dynamic Links is a powerful tool for increasing user engagement and directing users to the right content with customizable and dynamically generated deep links. By combining Flutter and Firebase, you can develop impressive and interactive mobile applications. You can explore the documentation of Firebase Dynamic Links to learn more about its features and details.
Here Let’s We look App
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_dynamic_links/firebase_dynamic_links.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Dynamic Links Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
String deepLink = '';
@override
void initState() {
super.initState();
handleDynamicLinks();
}
Future<void> handleDynamicLinks() async {
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData? dynamicLink) async {
final Uri? link = dynamicLink?.link;
if (link != null) {
setState(() {
deepLink = link.toString();
});
}
},
onError: (OnLinkErrorException e) async {
print('Dynamic Link Error: ${e.message}');
},
);
final PendingDynamicLinkData? data =
await FirebaseDynamicLinks.instance.getInitialLink();
final Uri? deepLink = data?.link;
if (deepLink != null) {
setState(() {
this.deepLink = deepLink.toString();
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Dynamic Links Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Deep Link:',
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
Text(
deepLink,
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
],
),
),
);
}
}