Flutter Firebase Dynamic Link Navigator.push not navigating

1.6k views Asked by At

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
1

There are 1 answers

1
Omatt On BEST ANSWER

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.