I have created a custom button widget in flutter called BottomButton. Since I feel that the implementation of the custom widget is not necessary for the question, I have simply omitted it.
BottomButton(
text: "Tap to Login",
onPressed: () {
errorMessage = LoginError.errorMessage;
if (errorMessage != null) {
SnackBar errorSnackBar = SnackBar(
content: Text(
LoginError.errorMessage,
textAlign: TextAlign.center,
));
setState() {
errorMessage = null;
LoginError.errorMessage = null;
}
Scaffold.of(context).showSnackBar(errorSnackBar);
}
loginWithEmailPassword();
},
)
In my application, after I call the Backend APi to authenticate a user, if there is an error authenticating the user I add it to LoginError.errorMessage. LoginError is a dart class and errorMessage is a static property of that class.
In the above code, what I am doing is upon tapping on my custom button widget to login the user, if there is an error, I am importing the error message I have added to the LoginError.errorMessage and displaying it on a SnackBar.
However if there is an error message, I reset the LoginError.errorMessage back to null and if the LoginError.errorMessage is null, I have written the code so that the SnackBar will not be shown. (The SnackBar is only shown if the LoginError.errorMessage is not null since I am doing an if check).
The current implementation shows error messages upon entering invalid credentials by the user. However if I enter correct credentials after entering the incorrect credentials and tapping on the custom Button widget once, the user is logged in, but the Invalid Credentials error is also show n on the snack bar.
I can not figure out why it is showing that incorrect credentials error on the Snackbar when I am trying to login with valid credentials after typing incorrect credentials once.