在Flutter中完成“ build”功能时是否有任何回调告诉我?

在Flutter中完成“ build”功能时是否有任何回调告诉我?,第1张

在Flutter中完成“ build”功能时是否有任何回调告诉我? 一般解决方案

只是为了澄清问题,我没想到这个问题会引起如此多的关注。因此,我只针对这个非常特殊的情况回答。
正如在另一个答案中解释的那样,

WidgetsBinding
提供了一种添加 一次性 框架 回调的方法

WidgetsBinding.instance.addPostframeCallback((_) {  // executes after build})

由于此回调仅被调用一次,因此您希望在每次构建时都添加它:

@overrideWidget build(BuildContext context) {  WidgetsBinding.instance.addPostframeCallback((_) => afterBuild);  return Container(); // widget tree}void afterBuild() {  // executes after build is done}
具体(异步)

详细阐述Günter的评论:

@overrideWidget build(BuildContext context) {  executeAfterBuild();  return Container();}Future<void> executeAfterBuild() async {  // this pre will get executed after the build method  // because of the way async functions are scheduled}

有一个很好的例子 说明这种效果
在这里



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5040105.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存