Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 检查Firestore数据库中是否已存在用户名_Firebase_Flutter_Google Cloud Firestore - Fatal编程技术网

Firebase 检查Firestore数据库中是否已存在用户名

Firebase 检查Firestore数据库中是否已存在用户名,firebase,flutter,google-cloud-firestore,Firebase,Flutter,Google Cloud Firestore,在我的应用程序中,我必须实现屏幕上的签名 一切都很好,但有一点不行。当有人想注册时,他不能像其他用户一样使用同一封电子邮件,这是可行的,但我也希望他不能使用相同的用户名。我在CloudFireStore中有一个名为“SerX”的集合和一个名为“用户名”的文档字段。如果有人知道怎么做,请评论你的解决方案 应该有这样的返回“此用户名已存在!” Widget _buildUserNameTF() { return Column( crossAxisAlignment: Cros

在我的应用程序中,我必须实现屏幕上的签名

一切都很好,但有一点不行。当有人想注册时,他不能像其他用户一样使用同一封电子邮件,这是可行的,但我也希望他不能使用相同的用户名。我在CloudFireStore中有一个名为“SerX”的集合和一个名为“用户名”的文档字段。如果有人知道怎么做,请评论你的解决方案

应该有这样的返回“此用户名已存在!”

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,
            ),
          ),
        ),
      ],
    );
  }
Widget\u buildUserNameTF(){
返回列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
正文(
“用户名”,
风格:克拉贝尔风格,
),
尺寸箱(高度:10.0),
容器(
对齐:alignment.centerLeft,
装饰:kBoxDecorationStyle,
身高:60.0,
子项:TextFormField(
键盘类型:TextInputType.text,
验证器:(输入){
if(input.isEmpty){
返回“请输入用户名”;
}
如果(输入长度<6){
返回“您的用户名至少需要6个字符”;
}否则如果(input.length>12){
返回“您的用户名最多需要12个字符”;
}
if(!RegExp)(
r'^[a-zA-Z0-9]+$)
.hasMatch(输入)){
返回“请输入有效的用户名”;
}
},
onSaved:(输入)=>\u用户名=输入,
样式:TextStyle(
颜色:颜色,黑色,
Fontron家族:“轨道飞行器”,
),
装饰:输入装饰(
边框:InputBorder.none,
contentPadding:仅限边集(顶部:14.0),
前缀:图标(
图标。受监督的用户圈,
颜色:颜色,黑色,
),
hintText:'输入您的用户名',
hintStyle:kHintTextStyle,
),
),
),
],
);
}

这里有一个简单的函数,用于检查客户端是否已经存在用户名

  var _instance = Firestore.instance;

  Future<bool> userExists(String username) async =>
      (await _instance.collection("users").where("username", isEqualTo: username).getDocuments()).documents.length > 0;
var\u instance=Firestore.instance;
未来用户存在(字符串用户名)异步=>
(wait_instance.collection(“users”).where(“username”,isEqualTo:username).getDocuments()).documents.length>0;

客户端检查不是一个安全的解决方案。您应该使用它。

按用户名筛选用户数据库。如果结果为“无”,则表示用户名不存在。你也可以用。你能给我举个例子吗?:)但是我如何用if函数来检查呢?当我按下注册按钮时?:)你是什么意思?比如当我按下注册按钮时,函数会检查用户名是否存在。当用户名存在时,返回“用户名已存在!”你可能想看一些关于基本登录流等的教程。这个问题是关于小部件和状态管理的核心。好的,但是如果用户名存在,你怎么能ceck??如果(userExists(input)==true){返回'这个用户名已经存在!';},我会这样想