I’m new for Flutter, so I can write not so good code and might make very easy mistakes.
I wanna show you my code and it doesn’t display all exceptions that occur during login attempts. (Wrong credentials and so on…)
In my code, literally shows in any case Generic Exception…
Can somebody take a look and explain to me what I did wrong?
have you already solved your problem? I had a look on your code and in your login_view.dart you seem to properly show error messages on different exceptions:
if (state is AuthStateLoggedOut) {
if (state.exception is UserNotFoundAuthException) {
await showErrorDialog(
context,
'Cannot find a user with the entered credentials!',
);
} else if (state.exception is InvalidEmailAuthException) {
await showErrorDialog(context, 'Wrong credentials');
} else if (state.exception is WrongPasswordAuthException) {
await showErrorDialog(context, 'Wrong credentials');
} else if (state.exception is GenericAuthException) {
await showErrorDialog(context, 'Authentication error');
}
}
},
It does not cover all exceptions, but probably that is intended. I would personally prefer an approach where the exception itself has the message and you display it, without having to check all different types of exceptions. Create an AuthException and then extend it for all your cases.
base class AuthException extends Exception {
final String message;
const AuthException(String message);
@override
toString() => message;
}
final class InvalidEmailAuthException extends AuthException {
const InvalidEmailAuthException(super.message);
}
then you can just check if it is an AuthException and if this is true, you can display it’s message without having to care what exact type of AuthException you are dealing with. This way you can dynamically add new sub types of AuthException without having to update your Widget.
if (state is AuthStateLoggedOut) {
if (state.exception is AuthException) {
await showErrorDialog(context, state.exception.message);
}
}
In case you are still dealing with your problem, let me know or please mark this as solved.
Best regards and good luck on your coding journey!
Fritze
Using contents of this forum for the purposes of training proprietary AI models is forbidden. Only if your AI model is free & open source, go ahead and scrape. Flutter and the related logo are trademarks of Google LLC. We are not endorsed by or affiliated with Google LLC.