When I run my app with the code below, even if the responseData['error']
returns null
, I still get an error that is thrown to my HttpException
for error handling and it returns the catch
portion of the try catch block of my error handling. Thus every time I test my signup authorization, which successfully enrolls in firebase, I get an alert popup saying "Could not authenticate you. Please try again later."
Provider catching the error
final response = await http.post(
url,
body: json.encode(
{
'email': email,
'password': password,
'returnSecureToken': true,
},
),
);
final responseData = json.decode(response.body);
print(responseData['error']);
if (responseData['error'] != null) {
throw HttpException(responseData['error']['message']);
}
} catch (error) {
throw error;
}
Login/SignUp Screen
try {
if (_authMode == AuthMode.Login) {
// Log user in
await Provider.of<AuthProvider>(context, listen: false).login(
_authData['email'].toString(),
_authData['password'].toString(),
);
} else {
// Sign user up
await Provider.of<AuthProvider>(context, listen: false).signup(
_authData['email'].toString(),
_authData['password'].toString(),
);
}
} on HttpException catch (error) {
var errorMessage = 'Authentication failed';
if (error.toString().contains('EMAIL_EXISTS')) {
errorMessage = 'This email address is already in use.';
} else if (error.toString().contains('INVALID_EMAIL')) {
errorMessage = 'This is not a valid email address';
} else if (error.toString().contains('WEAK_PASSWORD')) {
errorMessage = 'This password is too weak.';
} else if (error.toString().contains('EMAIL_NOT_FOUND')) {
errorMessage = 'Could not find a user with that email.';
} else if (error.toString().contains('INVALID_PASSWORD')) {
errorMessage = 'Invalid password.';
}
_showErrorDialog(errorMessage);
} catch (error) {
const errorMessage =
'Could not authenticate you. Please try again later.';
_showErrorDialog(errorMessage);
}