如何使用go_router使用Flutter Firebase动态链接

发布于 2025-01-24 07:58:06 字数 383 浏览 0 评论 0原文

我有firebase 动态链接在我的应用中集成并正常工作,后来我更改为使用go_router,现在我不知道该怎么办做它可以正常工作或如何处理。

我想象的方式是,FB动态链接PATH将与pathgorouter 中的页面相同。路由,而go_router将自动自动重定向,但我认为这不是其工作方式,我找不到任何资源。

因此,问题是如何使用使用使用go_router使用firebase dynamiclinks

I had firebase dynamic links integrated and working fine in my app and later i changed to use go_router and now i don't know what i should do to get it working or how to handle it.

The way i imagined it would be is that the FB dynamic link path will be the same as the path to the page in GoRouter routes, and go_router will redirect to the page automatically but i don't think that is how it works and i can't find any resources for this.

So the question is how to use Firebase DynamicLinks with go_router?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2025-01-31 07:58:06

这就是我的做法,看起来工作正常:)

  • 当应用处于终止模式时:

    future main()async {
    ...

      //封闭状态:使用GetInitialLink
    最终pendenddynamiclinkdata? initiallink =
        等待firebasedynamiclinks.instance.getinitiallink();
     

    ...

然后通过runapp(mtapp(initialRoute:initialLink))将其传递给gorouter.initiallocation字段或gorouter.redirect函数,具体取决于您的使用方式。

  • 因为当该应用在后台时:

将您的顶级小部件用您创建的状态窗包装,例如:

ahdynamiclinksmanager cass fording

 StatefulWidget {
  final Widget child;
  const AHDynamicLinksManager({required this.child, Key? key})
      : super(key: key);

  @override
  State<AHDynamicLinksManager> createState() => _AHDynamicLinksManagerState();
}



class _AHDynamicLinksManagerState extends State<AHDynamicLinksManager> {

  @override
  void initState() {
    super.initState();
    FirebaseDynamicLinks.instance.onLink.listen((dynamicLinkData) {
      String goingTo = dynamicLinkData.link.path;
      GoRouter.of(context).go(goingTo);
    }).onError((error) {
      GoRouter.of(context).go("/errorpage/$error");
    });
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

希望它能有所帮助!

this is how I did it, looks like working fine :)

  • For when the app is in terminated mode:

    Future main() async {
    ...

    // Closed state: getInitialLink is Used
    final PendingDynamicLinkData? initialLink =
        await FirebaseDynamicLinks.instance.getInitialLink();
    

    ...

then through runApp(MtApp(initialRoute: initialLink)) pass it to GoRouter.initialLocation field or GoRouter.redirect function, depending on how you use it.

  • For when the app is in background:

wrap your top widget with a statefulWidget your created like:

class AHDynamicLinksManager extends

 StatefulWidget {
  final Widget child;
  const AHDynamicLinksManager({required this.child, Key? key})
      : super(key: key);

  @override
  State<AHDynamicLinksManager> createState() => _AHDynamicLinksManagerState();
}



class _AHDynamicLinksManagerState extends State<AHDynamicLinksManager> {

  @override
  void initState() {
    super.initState();
    FirebaseDynamicLinks.instance.onLink.listen((dynamicLinkData) {
      String goingTo = dynamicLinkData.link.path;
      GoRouter.of(context).go(goingTo);
    }).onError((error) {
      GoRouter.of(context).go("/errorpage/$error");
    });
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}

Hope it helped!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文