Flutter
[Flutter] showDialog 안될때
_annie_
2021. 11. 9. 20:30
728x90
======== Exception caught by gesture ===============================================================
The following assertion was thrown while handling a gesture:
No MaterialLocalizations found.
ToDoApp widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
The material library uses Localizations to generate messages, labels, and abbreviations.
To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to include them automatically, or add a Localization widget with a MaterialLocalizations delegate.
코드상으로는 문제가없어서 빌드가 되지만, 실제 작동시킬때 위와 같은 에러가 뜨면서 Dialog가 안보일 때가 있다.
전에 showDialog만 테스트했을때는 잘 됐었는데 왜 갑자기 안되는지 무슨 차이가 있는지 생각하기 시작.
뭔가 다이얼로그가 어떤 화면 위에 떠야하는데 부모 위젯을 잘 못찾아서 그런게 아닐까란 생각에 도달했다
그래서 부모 위젯 정보를 담고있는 context 에 문제가 있는게 아닐까 생각하게되었고 관련 정보를 찾아보았다.
해결방법은 Builder를 끼워넣어서 상위로 만들어 준 다음 context를 받았다.
return Builder(
builder: (BuildContext ctx) {
return Scaffold(
child:
//생략
showDialog(
context: ctx,
builder: (
BuildContext context) =>
AlertDialog(
title: Text("중복된 항목이 있습니다."),
content: Text(value[0]['content'].toString()),
actions: [
TextButton(
child: Text('OK'),
onPressed: () {
Navigator.pop(
context, "OK");
},
),
],
)
,);
//생략
)
}
)
//scaffold의 child에서 showDialog를 시도 -> 실패
scaffold상위에 Builder로 감싸주었다.
이렇게 해결하거나 따로 위젯을 생성(분리) 하게되면 해결이 된다.
참고 : https://darrengwon.tistory.com/371
728x90
반응형