【发布时间】:2020-08-12 22:48:42
【问题描述】:
我正在尝试检查我的 Flutter 和 Firebase 应用程序中是否已使用用户名,但我遇到了一些问题。 我能够查询用户的集合,并将文本表单输入中保存的名称字符串与数据库中已有的名称进行比较,效果非常好。但我的问题是,当用户取名并且同一用户尝试保存他的个人资料而不更改他的名字时,他会收到消息用户名已被占用,这意味着他无法保存他的个人资料,因为他已经取了自己的名字。 我想要一个解决方案,如果用户没有修改他或她的用户名而不经过用户名已经被处理,他或她可以保存他的个人资料。不知道是否可以添加if语句来检查当前用户名是否等于文本字段中的用户名?或帮助任何有效的解决方案。 请这些是我下面的代码
//checks fields of user collection and compares the
//string _username with the userName of each user.
final QuerySnapshot result = await Firestore.instance
.collection('users')
.where('userName', isEqualTo: _userName)
.getDocuments();
//converts results to a list of documents
final List<DocumentSnapshot> documents =
result.documents;
//checks the length of the document to see if its
//greater than 0 which means the username has already been taken
the name
if (documents.length > 0) {
print(_userName + ' is already taken choose
another name');
}else{
print(_userName + ' are you sure you want to use
this name');
}
【问题讨论】:
标签: firebase flutter google-cloud-firestore