Flutter firebase 无法直接登陆子页面

Flutter firebase unable to directly land in subpage

大家好Flutter/Firebase,

我正在尝试实现一个简单的游戏,它接受 firebase 动态 links 和到应用程序特定页面的路由。

用例:

具有路由器的主页,该路由器将根据动态 link 请求将页面路由到应用程序

场景:

用户从 phone 的 Web 浏览器点击动态 link,如果用户有一个应用程序并登陆特定页面,应用程序就会打开。

我能做什么?

我可以在应用程序中打开特定页面。然而,当用户点击动态 link url 后,它首先打开应用程序的主页,然后被重定向到子页面。首次登陆首页后,有1-2秒的延迟才能登陆到具体页面。我想要的是省略用户登陆主页,只登陆动态 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!

你可以做的只是在你的第一个屏幕上显示一个加载屏幕(可能类似于启动屏幕?)。

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

        ));
      } 

代码已更新:

  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(),
            ),
          ));
        }
      }