[英]Showing firebase auth error codes in flutter UI
I'm new to flutter.So I was wondering if there is anyway that I can show the firebase auth error codes as the Errortext in textfield ?我是 flutter 的新手。所以我想知道是否可以将 firebase 身份验证错误代码显示为文本字段中的错误文本?
I am able to print the exception from firebase_auth but dont know how to give it as the errortext.我能够打印来自 firebase_auth 的异常,但不知道如何将其作为错误文本提供。
try {
final newuser = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: email ?? 'error',
password: password ?? 'error',
);
if (newuser != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ProfileDetail()),
);
}
} catch (e) {
print(e);
}
},
this is the textfield这是文本字段
TextField(
onChanged: (value) {
username = value;
},
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Username',
// errorText:
),
),
You can use "showsnackBar"您可以使用“showsnackBar”
try {
showDialog(
barrierDismissible: false,
context: context,
builder: (context) {
return const Center(
child:CircularProgressIndicator(),
);
});
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
Navigator.of(context)
.pushNamedAndRemoveUntil('/home', (route) => false);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('user-not-found')),
);
Navigator.of(context).pop();
} else if (e.code == 'wrong-password') {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('wrong-password')),
);
Navigator.of(context).pop();
}
First declare a variable to hold errorText;首先声明一个变量来保存errorText;
String? errorText;
Then extract the error like this,然后像这样提取错误,
try {
// try block
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
errorText = 'The provided password is too weak.';
} else if (e.code == 'email-already-in-use') {
errorText = 'The email is already in use.';
}
// set the value to your TextEditingController().
controller.text = errortext ?? "";
} catch (e) {
print(e);
}
You get common error codes from this link,您可以从此链接获得常见错误代码,
https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth-class.html https://pub.dev/documentation/firebase_auth/latest/firebase_auth/FirebaseAuth-class.html
Use on FirebaseAuthException
catch
like this.像这样在
FirebaseAuthException
上使用catch
。
try {
final newuser = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: email ?? 'error',
password: password ?? 'error',
);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
}
} catch (e) {
print(e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.