'Firebase Dynamic Link on getInitialLink in iOS (using Flutter)
I have the following code in my application:
For creating the dynamic link I have:
/// Function that creates a DynamicLink for a [SingleFashionItem] using [FirebaseDynamicLinks]
static Future<String> createFashionItemLink(
BuildContext context,
SingleFashionItem fashionItem,
) async {
// Get the package information, this function won't probably fail,
// therefore don't any errors
final PackageInfo packageInfo = await PackageInfo.fromPlatform();
final DynamicLinkParameters parameters = DynamicLinkParameters(
uriPrefix: CUSTOM_DOMAIN,
link: Uri.parse(
"$CUSTOM_DOMAIN/item?parentID=${fashionItem.parentID}&objectID=${fashionItem.objectID}"),
androidParameters: AndroidParameters(
packageName: packageInfo.packageName,
),
iosParameters: IosParameters(
bundleId: packageInfo.packageName,
appStoreId: "123456789" // TODO change this AppStoreID
),
socialMetaTagParameters: SocialMetaTagParameters(
title:
"${AppLocalizations.of(context).translate("check_out")} ${fashionItem.name}",
description: fashionItem.description,
imageUrl: Uri.parse(fashionItem.images[0].url),
),
);
// Build a short link
return parameters
.buildShortLink()
.then((shortDynamicLink) => shortDynamicLink.shortUrl.toString())
.catchError((error) {
print("The error is: ${error.toString()}");
return null;
});
}
// Function that handles the Dynamic Link using [FirebaseDynamicLinks]
static void handleDynamicLink(BuildContext context) async {
// Get the initial dynamic link if the app is started using the link
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.getInitialLink();
print("The dynamic data is ${data.toString()}");
// Handle the dynamic link
_handleDynamicLink(context, data);
// Handle when the application is brought to the foreground when being in background
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData data) async {
_handleDynamicLink(context, data);
}, onError: (OnLinkErrorException error) async {
// Send the error to Crashlytics
FirebaseCrashlytics.instance.reportNonFatalError(
exception: error,
customMessage: "Error while handling the dynamic link: ${data.link}",
userID: "not-available",
);
});
And for handling the dynamic link:
/// Function that handles the incoming [PendingDynamicLinkData]'s deep link
static void _handleDynamicLink(
BuildContext context, PendingDynamicLinkData data) {
final Uri deepLink = data?.link;
print("The deep link is: ${deepLink.toString()}");
// Check that the deep link is null or not
if (deepLink != null) {
// Handle deepLink
var isItem = deepLink.pathSegments.contains("item");
// Check if the items path segment is contained inside the path segment
if (isItem) {
// The deep link contains the items path segment
final objectID = deepLink.queryParameters["objectID"];
final parentID = deepLink.queryParameters["parentID"];
// Check that the itemID string is not null
if (objectID != null && parentID != null)
_goToDetailedFashionItemScreen(
context,
selectedFashionItemID: objectID,
parentID: parentID,
);
}
}
}
-> I've added in my
- Associated Domains applinks:example.com
- URL schemes com.example.application
- In the Info.plist
<key>FirebaseDynamicLinksCustomDomains</key>
<array>
<string>https://example.com/item</string>
</array>
What I'm getting in a null value from the:
final Uri deepLink = data?.link
From what I've seen in this Github issue it might be because of a race condition, however this doesn't work for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|