플러터 This Overlay widget cannot be marked as needing to build 문제 해결

플러터로 앱을 개발하다가 This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets.라는 에러 메시지를 마주할 때가 있습니다. 다음은 문제 해결하는 방법에 대해 작성합니다.

에러원인

이 에러는 Flutter의 위젯 트리에서 Overlay 위젯이 이미 빌드 프로세싱 중일 때 다시 빌드를 요청했을 때 발생합니다. 이로 인해 상태 관리 또는 화면 전환에서 예기치 못한 동작이 발생할 수 있습니다.

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.

기존코드

초기 위젯 빌드가 완료된 후 특정 작업을 수행해야 할 때가 있습니다.

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    //사용하고 있는 함수;
    _initialize();
  }

수정된 코드

이 경우 SchedulerBinding.instance.addPostFrameCallback를 사용하면 쉽게 해결할 수 있습니다. addPostFrameCallback 메서드는 Flutter 프레임워크에서 현재 프레임이 완료된 후 호출될 콜백 함수를 추가할 때 사용됩니다. 이 메서드는 첫 번째 프레임이 렌더링된 후, 즉 모든 위젯의 초기 빌드가 완료된 시점에 특정 작업을 수행하기에 적합합니다.

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    SchedulerBinding.instance.addPostFrameCallback((_) {
         //사용하고 있는 함수;
         _initialize();
    });
  }

Leave a Comment