-
[Error] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.Flutter/Error 2023. 6. 23. 10:59
E/flutter ( 3423): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter ( 3423): At this point the state of the widget's element tree is no longer stable.이라는 오류를 직면하게 되는 경우가 있을 것이다.
나 같은 경우는
Future.delayed(Duration(seconds: 2), () { Navigator.of(context).pop(false); });
버튼을 눌렀을 때 showDialog를 경고창을 띄우고 2초 뒤에 사라지길 원했다. 그리고 위와 같은 오류를 만났다.
이 오류는 프레임워크가 위젯 트리를 더 이상 사용하여 관련 정보를 검색할 수 없을 때 나타난다.
쉽게 말해서 두 가지로 말하자면,
1. CallBack에서 상태를 변경할 때 상태 객체가 이미 위젯 트리에서 제거 되었을 때
2. 비동기 함수가 완료되고 변경 사항을 알릴 때 위젯 트리가 이미 제거 되었을 때
대표적이로 이 두 가지 이유로 오류가 나타난다. 이 오류를 해결하기 위해서는 mounted 속성을 사용하여 위젯이 위젯 트리에 남아 있는지 확인하면 된다. 만약 위젯이 위젯 트리에 있다면, 위젯 트리를 안전하게 탐색할 수 있다.
Stateful 위젯의 를 사용하여 위와 같은 오류를 해결하는 예 코드이다.
if (mounted) { Future.delayed(Duration(seconds: 2), () { Navigator.of(context).pop(false); }); }
해결!
'Flutter > Error' 카테고리의 다른 글