只是为了澄清问题,我没想到这个问题会引起如此多的关注。因此,我只针对这个非常特殊的情况回答。
正如在另一个答案中解释的那样,
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}
有一个很好的例子 说明这种效果
在这里。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)