setState() or markNeedsBuild() called during build.处理办法

Flutter Jun 29, 2020
setState() or markNeedsBuild() called during build.
This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.

大概意思就是不能再在build时调用setState()markNeedsBuild()方法。

解决办法:

@override
void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_){
      /// setState操作
    });
}

Tags