대화 상자에서 버튼을 누른 후 로딩 화면을 시도했습니다. 대화 상자에서 버튼을 누르면 프로그램이 먼저 대화 상자를 표시 한 다음 2 초 동안 WillPopScope 개체를 표시 한 다음로드 화면을 표시합니다. 코드는 다음과 같습니다.
onPressed: () async {
Navigator.pop(context);
loadingScreen(context);
await Future.delayed(Duration(seconds: 2));
Navigator.pop(context);
},
하지만 다음과 같은 오류가 표시됩니다.
E/flutter (14960): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter (14960): At this point the state of the widget's element tree is no longer stable.
E/flutter (14960): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
몇 가지 연구 끝에 존재하지 않는 팝업 컨텍스트로 인해이 오류가 발생했다고 생각하므로 지연 후 대화 상자와 로딩 화면을 동시에 팝업하려고했습니다.
onPressed: () async {
loadingScreen(context);
await Future.delayed(Duration(seconds: 2));
Navigator.pop(context);
Navigator.pop(context);
},
작동하지만 정확히 내가 원하는 것은 아니지만 그 이유가 정말로 혼란 스럽습니다. 아무도 나에게 무슨 일이 일어나고 있는지 설명해 주시고 해결할 방법이 있습니까?
전체 코드 :
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Future<void> loadingScreen(BuildContext context) async {
return showDialog(
context: context,
builder: (context) {
return WillPopScope(
onWillPop: () => Future.value(false), child: Text(''));
});
}
dialog(BuildContext context) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text('Show Loading Screen'),
actions: <Widget>[
TextButton(
child: Text('Okay'),
onPressed: () async {
Navigator.pop(context);
loadingScreen(context);
await Future.delayed(Duration(seconds: 2));
Navigator.pop(context);
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Demo'),
),
body: Center(
child: TextButton(
child: Text('Show Dialog'),
onPressed: () {
dialog(context);
})));
}
}
주요 오류는 변수 이름 지정에 있습니다. 대화 상자 함수를
Widget build(BuildContext context)...
해당 함수에 대한 인수로 BuildContext가 있고 해당 컨텍스트를 대화 상자에 사용합니다. 더 잘 이해할 수 있도록 변수 이름을 변경하겠습니다.
dialog(BuildContext contextFromBuild) { //this is the argument to the function
return showDialog(
context: contextFromBuild,
builder: (BuildContext contextInsideDialog) { //new BuildContext inside the dialog
return AlertDialog(
content: Text('Show Loading Screen'),
actions: <Widget>[
TextButton(
child: Text('Okay'),
onPressed: () async {
Navigator.pop(contextInsideDialog);
loadingScreen(contextInsideDialog);
await Future.delayed(Duration(seconds: 2));
Navigator.pop(contextInsideDialog);
},
),
],
);
},
);
보시다시피 contextInsideDialog를 표시하고 있지만 다음에서 액세스하려고합니다.
loadingScreen(contextInsideDialog);
이미 해당 컨텍스트 (contextInsideDialog)를 팝했기 때문에 오류가 발생합니다. 변수 이름을 변경하여 문제를 해결하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다