Flutter setState() or markNeedsBuild() called when widget tree was locked.

https://stackoverflow.com/questions/45409565/flutter-setstate-or-markneedsbuild-called-when-widget-tree-was-locked

https://github.com/flutter/flutter/issues/89554

出现这个错误的原因大多数是当前页面的弹出框,弹出层并没有关闭就直接调用setState进行刷新Flutter组件导致的错误信息。
或者是你在构建阶段调用了setState导致的错误信息。

可以先关掉弹出层后,再进行setState的调用,或者使用

WidgetsBinding.instance
        .addPostFrameCallback((_) => setState(() {}));

WidgetsBinding.instance.addPostFrameCallback或Future.microTask()或Timer.run或Future.delayed(Duration.zero,三择其一的方式就可以。

Flutter关于WidgetsFlutterBinding API的介绍

posted @ 2024-12-20 09:06  星小梦  阅读(1)  评论(0编辑  收藏  举报