【发布时间】:2021-04-29 18:21:02
【问题描述】:
所以我尝试在我的颤振应用程序中使用 NodeJS 登录,并且我能够登录,但是当我在等待后端进程完成时尝试添加循环进度指示器时,当用户通过设置状态点击登录按钮时喜欢,
setState(() {
validate = true;
isLoading = true;
});
然后我登录成功后做,
setState(() {
validate = true;
isLoading = false;
});
我收到此错误:
error is Looking up a deactivated widget's ancestor is unsafe. I/flutter ( 4699): At this point the state of the widget's element tree is no longer stable. I/flutter ( 4699): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor
我无法导航到另一个屏幕,但如果我删除 setState,我将成功导航到另一个屏幕。
我的登录按钮点击代码如下。 isLoading 以 false 开头,当它为 true 时,我会显示一个圆形进度指示器。
Future saveLogInData(BuildContext context) async {
setState(() {
isLoading = true;
});
final formState = _logInFormKey.currentState;
if (formState.validate() ) {
formState.save();
try {
Map<String, String> data = {
"email": loginEmailController.text,
"password": loginPasswordController.text,
};
print("my data is : $data");
var response = await networkHandler.post("user/login", data);
print("statuscode is ${response.statusCode}");
if(response.statusCode == 200 || response.statusCode == 201){
Map<String, dynamic> output = json.decode(response.body);
print(output['token']);
setState(() {
validate = true;
isLoading = false;
});
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Home(
)));
}
else{
String output = json.decode(response.body);
print("output is ${output}");
setState(() {
validate = false;
isLoading = false;
});
}
// user.sendEmailVerification();
} catch (e) {
print("error is $e}");
} finally {
isLoading = false;
}
} else {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Error"),
content: Text("Please check your email and password"),
actions: <Widget>[
FlatButton(
child: Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
}
}
【问题讨论】: