Flutterfire: Firebase dynamic links : onLink callBack goes into infinite loop

Created on 21 May 2020  Â·  4Comments  Â·  Source: FirebaseExtended/flutterfire

void retrieveDynamicLink() async {
    final PendingDynamicLinkData data =
    await FirebaseDynamicLinks.instance.getInitialLink();
    final Uri deepLink = data?.link;
    print("Url: "+deepLink.toString());
    if (deepLink != null) {
      await deepLinkRedirectionSwitch(deepLink, context);
      /*setState(() {
        isDeepLinkRedirected= true;
      });*/
    }
    FirebaseDynamicLinks.instance.onLink(
        onSuccess: (PendingDynamicLinkData dynamicLink) async {
          final Uri deepLink = dynamicLink?.link;

          print("Url: "+deepLink.toString());

          if (deepLink != null && !isDeepLinkRedirected) {
            await deepLinkRedirectionSwitch(deepLink, context);
            /*setState(() {
              isDeepLinkRedirected= true;
            });*/
          }
        },
        onError: (OnLinkErrorException e) async {
          print('onLinkError');
          print(e.message);
        }
    );
  }

To Reproduce
1.Use firebase dynamic link

  1. create dynamic link
  2. Use adb shell am start -a android.intent.action.VIEW -d https://myapp.page.link/go
    Expected behavior
    deep link should be null if consumed.
    App should be in background or killed.

Flutter doctor
Run flutter doctor and paste the output below:

$ flutter doctor   
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.1, on Mac OS X 10.13.6 17G65, locale en-IN)

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
[!] Xcode - develop for iOS and macOS (Xcode 10.1)
    ✗ Flutter requires a minimum Xcode version of 11.0.0.
      Download the latest version or update via the Mac App Store.
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage
        on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install:
        sudo gem install cocoapods
[✓] Android Studio (version 3.4)
[!] IntelliJ IDEA Ultimate Edition (version 2018.1.8)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[!] VS Code (version 1.44.2)
    ✗ Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[✓] Connected device (1 available)

! Doctor found issues in 3 categories.

dynamic_links bug

Most helpful comment

For me, this happens in Android if the application is closed when the link is clicked. When the application is in the background, everything works fine. This is certainly a case of FirebaseDynamicLinks.instance.getInitialLink() not returning null after the first attempt. I have posted a minimal example here to reproduce this error.

All 4 comments

Hi @techyrajeev
What can you please provide actual result too?
can you please provide your flutter run --verbose?
Also, to better address the issue, would be helpful if you could post a minimal code sample to reproduce the problem, your code is incomplete and has missing code
Thank you

Its nothing more than below code. @TahaTesser

```
void main() {
runApp(MaterialApp(
title: 'Dynamic Links Example',
routes: {
'/': (BuildContext context) => MyHomeWidget(), // Default home route
'/helloworld': (BuildContext context) => MyHelloWorldWidget(),
},
));
}

class MyHomeWidgetState extends State {
.
.
.
@override
void initState() {
super.initState();
this.initDynamicLinks();
}

void retrieveDynamicLink() async {
final PendingDynamicLinkData data =
await FirebaseDynamicLinks.instance.getInitialLink();
final Uri deepLink = data?.link;
print("Url: "+deepLink.toString());
if (deepLink != null) {
await deepLinkRedirectionSwitch(deepLink, context);
/setState(() {
isDeepLinkRedirected= true;
});
/
}
FirebaseDynamicLinks.instance.onLink(
onSuccess: (PendingDynamicLinkData dynamicLink) async {
final Uri deepLink = dynamicLink?.link;

      print("Url: "+deepLink.toString());

      if (deepLink != null && !isDeepLinkRedirected) {
        await deepLinkRedirectionSwitch(deepLink, context);
        /*setState(() {
          isDeepLinkRedirected= true;
        });*/
      }
    },
    onError: (OnLinkErrorException e) async {
      print('onLinkError');
      print(e.message);
    }
);

}
.
.
.
}

so I had the same issue .. but I noticed that the issue is happening when deeplink.path was "/" which is redirecting it to "/".. which is putting in the infinite loop.. not sure why.. but when I put a if condition on not to redirect when deeplink.path is "/" then it worked perfectly fine.

Please check it out.. it may solve your problem too.

Also, I started getting the issue when added following intent in AndroidManifest.xml ( if anybody want to recreate the issue)

 <intent-filter android:autoVerify="true">
                <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.page.link"
                    android:scheme="https"/>
                <data
                    android:host="example.page.link"
                    android:scheme="http"/>
            </intent-filter>

For me, this happens in Android if the application is closed when the link is clicked. When the application is in the background, everything works fine. This is certainly a case of FirebaseDynamicLinks.instance.getInitialLink() not returning null after the first attempt. I have posted a minimal example here to reproduce this error.

Was this page helpful?
0 / 5 - 0 ratings