firebase - 您将如何检查 Flutter 中是否存在有关 Firebase 的用户名?
问题描述
// Checks for username in our registration authentication.
Future<bool> usernameCheck(String username) async {
final result = await FirebaseFirestore.instance
.collection('users')
.where('username', isEqualTo: username)
.get();
return result.docs.isEmpty;
}
这是我为检查 Firebase 的 Firestore 数据库中的用户名而创建的函数。此函数检查数据库中是否存在用户名。在下面的函数中,我将用户名转换为文本字符串,因为 signupNameController 是用户名的文本字段。它似乎不像预期的那样工作。
onPressed: () async {
if (usernameCheck(signupNameController.text) == true) {
showInSnackBar('Username exists. Please try again');
} else if (usernameCheck(signupNameController.text) == false) {
if (signupPasswordController.text != signupConfirmPasswordController.text) {
showInSnackBar("Passwords don't match.");
} else if (signupEmailController.text == ' ' ||
signupPasswordController.text == ' ' ||
signupNameController.text == ' ' ||
signupConfirmPasswordController.text == '') {
showInSnackBar("Please fill out all the required information.");
} else {
try {
UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: signupEmailController.text,
password: signupPasswordController.text,
);
// Our Database collection.
// This will go directly into our Firestore NoSQL database
FirebaseFirestore.instance.collection('users').doc().set({
'username': signupNameController.text.toLowerCase(),
'email': signupEmailController.text,
'password': signupPasswordController.text,
'retypePassword': signupConfirmPasswordController.text
});
Navigator.push(context, MaterialPageRoute(builder: (context) => TabNavigation()));
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
showInSnackBar("The password provided is too weak");
} else if (e.code == 'email-already-in-use') {
showInSnackBar("The account already exists for that email.");
}
} catch (e) {
showInSnackBar(e.toString());
}
}
}
这就是我想要完成的。它似乎不起作用或输入用户。有什么方法可以解决这个问题?我正在使用 Flutter 并将 Firebase 用于后端。