I want a dynamic error message instead of this static error message.
Please review my code and help me out with the code to show an error message in Flutter while logging in with Firebase.
I am making a login page for my app and I am using the Firebase authentication service for that. Now I am trying to show an error dialog message when a user attempts with the wrong credentials or any other case. And so for I have done this... I have coded this and it executes successfully but the error message is static "An unexpected error occurred", instead of this I want a dynamic error message which particularly shows what's wrong with the login credentials , for example, whether the email is badly formatted or password is wrong etc.
My code:
class LoginPage extends StatefulWidget {
final VoidCallback showRegisterPage;
const LoginPage({Key? key, required this.showRegisterPage}) : super(key: key);
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final formKey = GlobalKey<FormState>(); //key for form
String name = "";
final _emailController = TextEditingController();
final _passwordController = TextEditingController();
void showErrorMessage(Object error) {
String errorMessage;
if (error is String) {
errorMessage = error;
} else if (error is PlatformException) {
errorMessage = error.message ?? 'An unknown error occurred';
} else {
errorMessage = 'An unexpected error occurred';
}
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Error'),
content: Text(errorMessage),
actions: <Widget>[
ElevatedButton(
child: Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}
Future LoginPage() async {
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: _emailController.text.trim(),
password: _passwordController.text.trim(),
);
} catch (error) {
showErrorMessage(error);
}
}
void showErrorMessage(Object error){
check this snippet for ref.
This is a generic Authentication Method
but you can modify as needed. For a list of codes, see this
Future<String> loginUser({
required String email,
required String password,
}) async {
String res = 'Some error occurred';
try {
if (email.isNotEmpty || password.isNotEmpty) {
//Login user
await _auth.signInWithEmailAndPassword(
email: email, password: password);
res = 'success';
} else if (email.isEmpty && password.isEmpty) {
res = 'Please enter email and password';
} else if (email.isEmpty || password.isEmpty) {
res = 'Please enter email and password';
}
} on FirebaseAuthException catch (err) {
print(err);
if (err.code == 'user-not-found') {
res = 'No account associated with email address';
} else if (err.code == 'wrong-password') {
res = 'Password is incorrect';
} else if (err.code == 'invalid-email') {
res = 'Please enter valid email address';
} else {
res = 'Please enter valid email and password';
}
} catch (err) {
res = err.toString();
}
return res;
}
You need to catch the error codes given by firebase and then do checks to see what error was thrown and then display to the user.
Sign-up can be done with a similar approach. An example of error handling below:
on FirebaseAuthException catch (err) {
print(err);
if (err.code == 'invalid-email') {
res = 'The email address is badly formatted';
} else if(err.code == 'unknown') {
res = 'Please enter password';
} else if (err.code == 'weak-password') {
res = 'The password must be longer than 6 characters';
} else if (err.code == 'email-already-in-use') {
res = 'The email address already exists';
} else {
res = err.code.toString();
}
} catch (err) {
res = err.toString();
print(res);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.