我正在尝试为我的应用程序上的每个用户创建一个唯一的用户名。在注册时,我将该用户名值存储在一个单独的集合(“用户名”)中,因此在检查用户名是否唯一时更容易浏览它们,而不是浏览每个用户及其字段。
在我的 signup.dart 上,我有一个文本字段验证器:
TextFormField(
style: TextStyle(fontSize: 16.0, color: Colors.white),
decoration: buildSignInInputDecoration("Username"),
validator: (value) {
if (value.length < 2 || value.isEmpty) {
return "Username is too short.";
} else if (value.length > 12) {
return "Username is too long.";
} else {
return null;
},
onSaved: (value) => _username = value.trim(),
),
}
Run Code Online (Sandbox Code Playgroud)
我做了一个函数来检查用户名是否已经在集合中:(_username 是存储用户输入的字符串)
Future<bool> usernameCheck() async {
final snapShot = await usernamesRef.document(_username).get();
if (snapShot == null || !snapShot.exists) {
return true; //username is unique.
} else {
return …
Run Code Online (Sandbox Code Playgroud) dart firebase firebase-authentication flutter google-cloud-firestore