在我的应用中,我必须实现一个SignUpScreen。
一切正常,但一件事不行。当某人想要注册时,他不能像其他用户一样使用相同的电子邮件,这是可行的,但我也希望他不能使用相同的用户名。我在Cloud Firestore中有一个名为“ SerX”的集合和一个名为“ Username:”的文档字段。如果有人知道该怎么做,请评论您的解决方案。
应该有这样的回报:“此用户名已经存在!”
Widget _buildUserNameTF() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
'Username',
style: kLabelStyle,
),
SizedBox(height: 10.0),
Container(
alignment: Alignment.centerLeft,
decoration: kBoxDecorationStyle,
height: 60.0,
child: TextFormField(
keyboardType: TextInputType.text,
validator: (input) {
if (input.isEmpty) {
return 'Please enter a username';
}
if(input.length < 6){
return 'Your username needs to be at least 6 characters';
}else if(input.length > 12){
return 'Your username needs to be at most 12 characters';
}
if (!RegExp(
r'^[a-zA-Z0-9]+$')
.hasMatch(input)) {
return 'Please enter a valid username.';
}
},
onSaved: (input) => _Username = input,
style: TextStyle(
color: Colors.black,
fontFamily: 'Orbitron',
),
decoration: InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(top: 14.0),
prefixIcon: Icon(
Icons.supervised_user_circle,
color: Colors.black,
),
hintText: 'Enter your Username',
hintStyle: kHintTextStyle,
),
),
),
],
);
}
答案 0 :(得分:1)
这是一个用于检查用户名是否已经在客户端上的简单功能。
var _instance = Firestore.instance;
Future<bool> userExists(String username) async =>
(await _instance.collection("users").where("username", isEqualTo: username).getDocuments()).documents.length > 0;
客户端检查不是安全的解决方案。您应该使用Cloud Functions for Firebase。