So, I have created one snackbar widget in different dart file and I’m calling it in different dart file with context and text message. but when I’m getting error at that time snackbar is not showing on screen.
This is Separate dart file that contains Snackbar widget:
import 'package:flutter/material.dart';
import 'package:app/globals.dart' as globals;
class ErrorAlert{
showAlert(context, message) {
return ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: globals.mainColor,
content: Text(message),
duration: const Duration(seconds: 5),
action: SnackBarAction(
label: 'Dismiss',
textColor: Colors.white,
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
),
)
);
}
}
Here the Code from where I’m calling it (in catch part if condition):
signInWithEmailIdAndPassword(email,password, context) async {
try {
UserCredential result = await fireAuth.signInWithEmailAndPassword(email: email, password: password);
User user = result.user!;
// checkUser = user; //returns UserID
if(result != null) {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => const HomeScreen()));
}
}
catch(signError) {
if(signError is PlatformException) {
if(signError.code == 'ERROR_WRONG_PASSWORD') {
ErrorAlert().showAlert(context, 'Wrong Password!');
} else if(signError.code == 'ERROR_INVALID_EMAIL') {
ErrorAlert().showAlert(context, 'Invalid Email ID!');
} else if(signError.code == 'ERROR_ACCOUNT_EXISTS_WITH_DIFFERENT_CREDENTIAL') {
ErrorAlert().showAlert(context, 'Account is already exists with different credentials!');
} else if(signError.code == 'ERROR_USER_NOT_FOUND') {
ErrorAlert().showAlert(context, 'User not found!');
}
print(signError);
}
}
// return checkUser;
}
This is the debug message that is showing in console:
E/RecaptchaCallWrapper(32567): Initial task failed for action RecaptchaAction(action=signInWithPassword)with exception - The email address is badly formatted.
2
Answers
The exception you are trying to catch is not in the right
if
nesting.At the moment you are first trying to check if it is a
PlatformException
whereas Firebase is throwingFirebaseAuthException
You should try the following:
Also always check all the cases here you had no else status.
Additionally make sure to have the correct return codes for Firebase as if I remember correctly the invalid email code is
invalid-email
but you may already have constant that covers it.So for this is the problem with the context actually for better understanding you can refer this thread
Can someone explain to me what the Builder Class does in Flutter?
And to resolve your issue you can do it like this.