Flutter firebase unable to directly land in subpage

909

What you can do is just display a loading screen on your first screen (could be similar to a splash screen maybe? ) .

     @override
      Widget build(BuildContext context) {
        return (new MaterialApp(
          title: 'Game',
          home: Center(
            child: CircularProgressIndicator(),
          ),

        ));
      } 

Code updated:

  void main() {

        runApp(new MaterialApp(
          title: 'Game Name',
          home: _MainScreen(),
          routes: <String, WidgetBuilder>{
            '/NSef': (BuildContext context) => new SocialAppsPage(
                "page-1-title",
                "page-1"),
            '/82AY': (BuildContext context) => new SocialAppsPage(
                "page-2-title",
                "page-2"),
            '/DW7Y': (BuildContext context) => new SocialAppsPage(
                "page-3-title",
                "page-3"),
            '/core': (BuildContext context) => new GameHomePage()
          },  ));
      }

      class _MainScreen extends StatefulWidget {
        @override
        State<StatefulWidget> createState() => new _MainScreenState();
      }

      class _MainScreenState extends State<_MainScreen>  with WidgetsBindingObserver {

        Timer _timerLink;


        @override
        BuildContext get context => super.context;

       bool fromDynamicLink = false;

        verifyOrigin() async {
          await Future.delayed(Duration(seconds: 1));
          if (!fromDynamicLink){
            fromDynamicLink = true;
            Navigator.pushNamed(context, "/core");
          }
        }

          @override
        void initState() {
          WidgetsBinding.instance.addObserver(this);
          verifyOrigin();
          super.initState();
        }

        @override
        void didChangeAppLifecycleState(AppLifecycleState state) {
          if (state == AppLifecycleState.resumed) {
            fromDynamicLink = true;
            _retrieveDynamicLink();
          }
        }

        Future<void> _retrieveDynamicLink() async {
          final PendingDynamicLinkData data =
          await FirebaseDynamicLinks.instance.retrieveDynamicLink();
          final Uri deepLink = data?.link;

          if (deepLink != null) {
            Navigator.pushNamed(context, deepLink.path);
          } else {
            Navigator.pushNamed(context, "/core");
          }
        }

        @override
        void dispose() {
          WidgetsBinding.instance.removeObserver(this);
          if (_timerLink != null) {
            _timerLink.cancel();
          }
          super.dispose();
        }

        @override
        Widget build(BuildContext context) {
          return (new Scaffold(
            appBar: AppBar(title: Text('Game Name'),
            ),
            body: Center(
              child: CircularProgressIndicator(),
            ),
          ));
        }
      }
Share:
909
Bhupendra Acharya
Author by

Bhupendra Acharya

Updated on December 07, 2022

Comments

  • Bhupendra Acharya
    Bhupendra Acharya over 1 year

    Hello Flutter/Firebase folks,

    I am trying to implement a simple game that accepts firebase dynamic links and routes to the specific page of the app.

    Use case:

    A home page that has router which will route the page based on the dynamic link request to an app

    Scenario:

    User clicks on dynamic link from web browser of phone and the app opens given a user has an app and lands in the specific page.

    What I am able to do?

    I am able to open the specific page in the app. However, after user clicks on the dynamic link url, it first opens the home page of the app, and then gets redirected to the subpage. There is a delay of 1-2 second before landing to the specific page after first landing in the home page. What I would like - is to omit user landing in homepage and just land in the specific page of the dynamic link

    **Code Format:**
    
    Home Page : It has some Game
      
    
    The router has 4 different subpages : 
    
    Default Home Page that will show game, and the rest other pages are based on the dynamic link provided Ex: Page_1, Page_2, and Page_3. 
    
    **What am I looking help or guidance?**
    
    The suggestion I am looking is to avoid landing in home page (game landing page) when dynamic link is clicked from web url of the mobile.
    
    
    
    void main() {
    
      runApp(new MaterialApp(
        title: 'Game Name',
        home: _MainScreen(),
        routes: <String, WidgetBuilder>{
          '/NSef': (BuildContext context) => new SocialAppsPage(
              "page-1-title",
              "page-1"),
          '/82AY': (BuildContext context) => new SocialAppsPage(
              "page-2-title",
              "page-2"),
          '/DW7Y': (BuildContext context) => new SocialAppsPage(
              "page-3-title",
              "page-3"),
          '/core': (BuildContext context) => new GameHomePage()
        },  ));
    }
    
    class _MainScreen extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => new _MainScreenState();
    }
    
    class _MainScreenState extends State<_MainScreen>  with WidgetsBindingObserver {
    
      Timer _timerLink;
    
    
      @override
      BuildContext get context => super.context;
    
        @override
      void initState() {
        WidgetsBinding.instance.addObserver(this);
        super.initState();
      }
    
      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        if (state == AppLifecycleState.resumed) {
          _timerLink = new Timer(const Duration(milliseconds: 1), () {
            _retrieveDynamicLink();
          });
        }
      }
    
      Future<void> _retrieveDynamicLink() async {
        final PendingDynamicLinkData data =
        await FirebaseDynamicLinks.instance.retrieveDynamicLink();
        final Uri deepLink = data?.link;
    
        if (deepLink != null) {
          Navigator.pushNamed(context, deepLink.path);
        } else {
          Navigator.pushNamed(context, "/core");
        }
      }
    
      @override
      void dispose() {
        WidgetsBinding.instance.removeObserver(this);
        if (_timerLink != null) {
          _timerLink.cancel();
        }
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return (new MaterialApp(
          title: 'Game Name',
          home: Center(
            child: CircularProgressIndicator(),
          ),
          navigatorObservers: <NavigatorObserver>[observer],
          routes: <String, WidgetBuilder>{
            '/NSef': (BuildContext context) => new SocialAppsPage(
                "page-1-title",
                "Page-1"),
            '/82AY': (BuildContext context) => new SocialAppsPage(
                "page-2-title",
                "Page-2"),
            '/DW7Y': (BuildContext context) => new SocialAppsPage(
                "page-3-title",
                "page-3"),
            '/core': (BuildContext context) => new GameHomePage()
          },
        ));
      }
    }
    
    Any help or suggestions is appreciated.
    Thanks!