1

I am trying to implement Firebase Dynamic links in a flutter. But when I click on the link it calls the functions but does not take me to the specified page.

Code Implementation

main.dart

Main Entry Final for Application

void main() {

  Crashlytics.instance.enableInDevMode = true;
  FlutterError.onError = Crashlytics.instance.recordFlutterError;

  runZoned(() {

    runApp(MyApp());
  }, onError: Crashlytics.instance.recordError);



}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {

    DynamicLinks dynamicLinks = new DynamicLinks();
    dynamicLinks.initDynamicLinks(context);


    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
    return LayoutBuilder(builder: (context, constraints) {
      return OrientationBuilder(builder: (context, orientation) {
        SizeConfig().init(constraints, orientation);

        return MaterialApp(
          title: 'APP NAME',
          theme: ThemeData(
              primarySwatch: Colors.orange,
              brightness: Brightness.light,
          ),
          debugShowCheckedModeBanner: false,
            home:SplashScreenMain(),
        );
      });
    });
  }
}

dynamicLinkManager.dart

Another class to handle Dynamic Links.

class DynamicLinks {
  void initDynamicLinks(BuildContext context) async{


    var data = await FirebaseDynamicLinks.instance.getInitialLink();

    FirebaseDynamicLinks.instance.onLink(onSuccess: (dynamicLink)  async {
      print("Main = ${dynamicLink}");
      var deepLink = dynamicLink?.link;

      final queryParams = deepLink.queryParameters;

      debugPrint('DynamicLinks onLink $deepLink');
      print("queryParams $queryParams");

      if(DynamicLinksConst.inviteUser == deepLink.path){
        print("Step 1.......Code Works");
        
        /* THIS PART CODE IS NOT WORKING  */
        Login.setActiveContext(context);
        Navigator.push(context,
          EaseInOutSinePageRoute(
              widget: SignupPage()), //MaterialPageRoute
        );
      }else{
        Navigator.push(context,
          EaseInOutSinePageRoute(
              widget: LoginPage()), //MaterialPageRoute
        );
      }
    }, onError: (e) async {
      debugPrint('DynamicLinks onError $e');
    });

  }

 



}

Console Output Here is the output you can see that its returning data captured by dynamic link. I Don't Think it a problem with firebase dynamic link it feels like more of a Navigator problem but I am unable to identify the problem here as this Navigator is working properly throughout the project expect here.

EaseInOutSinePageRoute just adds animation to navigations.

I/flutter (  395): Main = Instance of 'PendingDynamicLinkData'
I/flutter (  395): DynamicLinks onLink https://example.com/abc?para1=dataOne
I/flutter (  395): queryParams {para1: dataOne}
I/flutter (  395): Step 1.......Code Works
Abhishek Kumar
  • 539
  • 9
  • 17
  • It looks like the issue here is with `Navigator.push()` and Dynamic Links works properly. Could you check if it's getting the expected BuildContext to push the target screen? – Omatt Oct 17 '20 at 13:53
  • @Omatt I think you're right. When I remove my **Authenticator** Class it works fine. The Authenticator is checking for user data in shared preference and pushing the new page on the base of it. After 3 seconds of wait on the splash screen Authenticator is called? so maybe it's overwriting the main context. How can I manage the context of main even if its overwritten by another page call? – Abhishek Kumar Oct 19 '20 at 09:06

1 Answers1

1

As mentioned in my comment, the issue here is that the expected BuildContext isn't used in Navigator.push().

Without a minimal repro, it's difficult to provide a concrete solution. Since you've mentioned that you're using an Authenticator class that pushes a new page/screen, it might be safer to manage the screen of the app in a single class. With this, it's easier to manage the BuildContext being used in Navigator.push(). You may check this sample in this blog post and see if it fits your use case.

Omatt
  • 8,564
  • 2
  • 42
  • 144
  • Thank you for sharing this looks promising ( the blog post it makes code much more manageable ), But I resolved it by adding navigatorKey in my material app and using it to navigate. But in long run, I will be changing it to onGeneratedRoute. – Abhishek Kumar Oct 20 '20 at 08:08