Skip to main content

Command Palette

Search for a command to run...

[Flutter] setState() called after dispose()

Updated
2 min read
D

Web / iOS / Flutter Developer

경고 메세지 분석

Unhandled Exception: setState() called after dispose(): _NoMoreItemState#dc583(lifecycle state: defunct, not mounted)

This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.

The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.

This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

앱에 타이머를 추가하거나, 애니메이션 위젯을 구현하다보면 종종 발견할 수 있는 warning 메세지이다. setState()가 실행되는 시점에 이미 해당 위젯이 dispose()된 상태인 경우, 이는 메모리 누수가 발생할 수 있다는 warning 메시지가 발생하게 된다.

이를 해결하기 위해서는 Timer를 onDispose()시 함께 dispose 해주거나 해당 위젯이 mounted인 상태일 경우에만 setState()를 호출할 수 있도록 로직을 수정해주어야한다.

다음 예제는 아이템 리스트를 불러오다가 마지막 아이템을 읽어온 후에 해당 아이템이 마지막 아이템임을 표시해주는 Text() 위젯이 AnimatedOpacity()위젯을 활용해 애니메이션 효과와 함께 출력되도록 하는 로직이다.


class NoMoreItem extends StatefulWidget {
  final String? label;
  const NoMoreItem({super.key, this.label});

  @override
  State<NoMoreItem> createState() => _NoMoreItemState();
}

class _NoMoreItemState extends State<NoMoreItem> with AfterLayoutMixin<NoMoreItem> {
  var _op = 0.0;

  @override
  FutureOr<void> afterFirstLayout(BuildContext context) async {
    await Future.delayed(const Duration(milliseconds: 400), () {
      if (mounted) {
        setState(() {
          _op = 1.0;
        });
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      margin: const EdgeInsets.only(top: 16, bottom: 10),
      alignment: Alignment.center,
      child: AnimatedOpacity(
        duration: const Duration(milliseconds: 400),
        opacity: _op,
        child: Text(
          widget.label ?? 'That\'s all for now!'.hardcoded,
          style: AppTextStyles.black.copyWith(
            color: Palette.gray6B,
            fontSize: 17.sp,
            fontWeight: FontWeight.w600,
          ),
        ),
      ),
    );
  }
}

예제에서 사용한 AfterLayout과 관련된 내용은 패키지 설명을 통해 확인할 수 있다.

After Layout Package

예제에서는 afterLayout() 내에서 위젯이 위젯 트리에 생성되고 UI가 display될 때 애니메이션이 실행될 수 있도록 일정한 딜레이를 주고 setState()를 실행한다.

@override
  FutureOr<void> afterFirstLayout(BuildContext context) async {
    await Future.delayed(const Duration(milliseconds: 400), () {
      if (mounted) {
        setState(() {
          _op = 1.0;
        });
      }
    });
  }

해당 부분에서 만약 mounted를 체크해주지 않고 setState()를 호출하게 되면 만약 사용자가 setState()가 실행되는 시점에 다른 페이지로 이동하는 경우가 발생하여 dispose()가 일어나게 될 경우 setState() called after dispose() 경고가 출력될 수 있으며, 메모리 누수가 발생할 수 있다.

if(mounted)를 통해 분기처리를 해주면 setState()함수가 위젯이 dispose()되지 않은 경우에만 실행되도록 설정할 수 있다.

edited by 김동한

More from this blog

[ZSH] tree 사용하기

들어가며 큰 규모의 프로젝트를 출시한 뒤, 후일을 위해서 더 늦기 전에 파일 정리 및 문서화를 진행해야했다. 문서화 작업을 하는 중에 기왕 정리하는 거 파일 구조를 이쁘게 트리 구조로 나열하여 코멘트를 달면 나중에 보더라도 이해하기 더 쉬울 것 같았다. 어떻게 해야 간지나는 트리 구조를 만들 수 있을까 방법을 찾다보니 역시나 파일 구조를 트리로 이쁘게 출력해주는 커맨드 툴이 존재했다. tree 커맨드에 대해서 알아보고 알짜배기 내용만 정리했다....

Feb 21, 20242 min read

[Next.js] parallel routes & intercepting routes

트위터 로그인 모달창을 만들어보며 넥스트의 parallel routes 와 intercepting routes 을 학습한 내용을 정리해보았습니다. 트위터 로그인 창을 확인해봅시다. 루트 디렉토리 화면을 배경으로 i/flow/login 페이지가 동시에 표시되고 있습니다. 저는 app router 를 학습하기 전까지는 createPortal 을 사용하여 포탈 영역에 로그인 컴포넌트를 띄우는 방식을 사용했었습니다. const NoLogin =()=...

Feb 1, 20244 min read

C/C++ 이진 트리(binary tree) 개요 및 구현(1)

개요 트리는 노드들이 나무 가지처럼 연결된 비선형 계층적 자료구조이다. 하위 트리가 존재하고, 그 노드에 또 하위 트리가 존재하는 자료구조 이다. 트리의 맨 위에 있는 루트 노드가 존재한다. 우리가 알아볼 트리는 이진 트리이다. 이진 트리는 자식 노드(부모로부터 아래로 이어진 노드)가 2개 이하인 구조를 말한다. 트리의 사용 사례로는 다음과 같다 계층 적 데이터 저장(파일,폴더) 효율적인 검색 속도 힙 데이터 베이스의 인덱싱 트리에 ...

Jan 31, 20244 min read

[React] Server component (RSC)

React.js 18 에 도입된 리액트 서버 컴포넌트는 서버에서 동작하는 리액트 컴포넌트를 의미합니다. Next가 권장하는 라우팅 방식인 app router의 기반이 되는 컴포넌트이기 때문에 app router 를 이해하기 위해서는 server component 에 대한 이해가 필요합니다. server component 리액트는 클라이언트단만을 컴포넌트화하는 대신, server component라는 개념을 통해 서버 영역을 컴포넌트화합니다. ...

Jan 29, 20243 min read

Flutter, JavaScript

42 posts