Flutter:在 Widget 上运行方法构建完成 [英] Flutter: Run method on Widget build complete

查看:29
本文介绍了Flutter:在 Widget 上运行方法构建完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在 Widget 完成构建/加载后运行函数,但我不确定如何运行.我当前的用例是检查用户是否通过身份验证,如果没有,则重定向到登录视图.我不想在之前检查并推送登录视图或主视图,它需要在主视图加载后发生.有什么我可以用来做这件事的吗?

I would like to be able to run functions once a Widget has finished building/loading but I am unsure how. My current use case is to check if a user is authenticated and if not, redirect to a login view. I do not want to check before and push either the login view or the main view, it needs to happen after the main view has loaded. Is there anything I can use to do this?

推荐答案

你可以使用

https://github.com/slightfoot/flutter_after_layout

在布局完成后只执行一次函数.或者只是查看它的实现并将其添加到您的代码中:-)

which executes a function only one time after the layout is completed. Or just look at its implementation and add it to your code :-)

基本上是

  void initState() {
    super.initState();
    WidgetsBinding.instance
        .addPostFrameCallback((_) => yourFunction(context));
  }

这篇关于Flutter:在 Widget 上运行方法构建完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆