我用 cognito 制作了一个登录表单,我想在登录成功或失败后通知用户,使用AlertDialog
. 用户尝试失败后,对话框显示,它弹回当前屏幕,在我按下警报对话框 ( OK
) 中的按钮后,它抛出错误Looking up a deactivated widget's ancestor is unsafe.
我不明白为什么会发生这种情况,我是新手颤抖所以不知道如何解决它。这是登录方法:
void loginToCognito() async {
final storage = FlutterSecureStorage();
await storage.write(key: "e", value: email);
await storage.write(key: "p", value: password);
String message;
try {
_user = await _userService.login(email, password);
message = 'User successfully logged in!';
if (!_user.confirmed) {
message = 'Please confirm user account';
}
return showAlertDialog(
context,
message,
Navigator.pushNamed(context, SecondScreen.id),
);
} on CognitoClientException catch (e) {
if (e.code == 'InvalidParameterException' ||
e.code == 'NotAuthorizedException' ||
e.code == 'UserNotFoundException' ||
e.code == 'ResourceNotFoundException') {
message = e.message;
/// This is where the error happpens
return showAlertDialog(
context,
message,
Navigator.pop(context),
);
} else {
message = 'An unknown client error occurred';
return showAlertDialog(
context,
message,
Navigator.pop(context),
);
}
} catch (e) {
message = 'An unknown error occurred';
return showAlertDialog(
context,
message,
Navigator.pop(context),
);
}
}
这是警报对话框:
showAlertDialog(
BuildContext context,
String message,
void function,
) {
// Create button
Widget okButton = FlatButton(
child: Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
);
// Create AlertDialog
AlertDialog alert = AlertDialog(
title: Text('User Login'),
content: Text(message),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
这个错误是因为第一次Navigator.pop(context);
发生吗?如果是这样,解决此问题的最佳方法是什么?提前致谢。我已经尝试过,final globalScaffoldKey = GlobalKey<ScaffoldState>();
但未能解决它的问题。