I've made a login form with cognito in flutter, and I wanna notify the users after a successful or failed attempt to login, with a AlertDialog
. After a user failed attempt, the dialog displays, it pops back to the current screen, and after I press the button in the alert dialog (OK
), it throws and error Looking up a deactivated widget's ancestor is unsafe.
I don't understand quite why is this happening, I'm new to flutter so not sure how to solve it.
This the login method:
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),
);
}
}
And here is the alertDialog:
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;
},
);
}
Is this error because the first Navigator.pop(context);
happened? If so, what is the best approach to fix this issue? Thanks in advance. I've tried with the final globalScaffoldKey = GlobalKey<ScaffoldState>();
but failed to fix the issue with it.