[英]How can i check username availability in my flutter firebase app
I am trying to check if a username is already taken in my flutter and firebase app, but I am facing some problems.我正在尝试检查我的 flutter 和 firebase 应用程序中是否已使用用户名,但我遇到了一些问题。 I am able to query the user's collection and compare a name string saved in the input of the text form with names already in the database, which works perfectly well.
我能够查询用户的集合,并将文本表单输入中保存的名称字符串与数据库中已有的名称进行比较,效果非常好。 But my problem is when a user takes a name and that same user tries to save his profile without changing his name, he gets the message username is already taken, meaning he can't save his profile cause he has already taken his own name.
但我的问题是,当一个用户取了一个名字,而同一个用户试图在不更改他的名字的情况下保存他的个人资料时,他会收到消息用户名已被使用,这意味着他无法保存他的个人资料,因为他已经取了自己的名字。 I want a solution where a user can save his profile if he or she hasn't modified his or her username without going through the username is already taken process.
我想要一个解决方案,如果用户没有修改他或她的用户名而不经过用户名已经采取的过程,他或她可以保存他的个人资料。 I don't know if it's possible to add an if statement to check if the current user name is equal to the username in the text field?
不知道可不可以加个if语句来判断当前用户名是否等于文本域中的用户名? or help with any solution that works.
或帮助解决任何有效的问题。 please these are my codes below
请这些是我下面的代码
//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');
}
I think a solution would to only check to see if this username is taken if the user changes his username.我认为一个解决方案是只检查如果用户更改了他的用户名,是否使用了这个用户名。 Therefore you would not have to worry if the user with the taken username is just saving his profile without changing his username.
因此,您不必担心使用已使用用户名的用户只是保存他的个人资料而不更改他的用户名。
yes if you are in firestore as backend...you can not show query stream in registration page for username suggestions for availability bcoz u hav to set firestore rules insecure and allow unauthorised users to read write your database...so better to push a new page after that registration page where you can place field for username in this case you are already registered and logged in so firestore security rule will not invade.... Happy coding:)是的,如果你在 firestore 作为后端......你不能在注册页面中显示查询 stream 以获取可用性的用户名建议,因为你必须将 firestore 规则设置为不安全并允许未经授权的用户读写你的数据库......所以最好推送一个在该注册页面之后的新页面,您可以在其中放置用户名字段,在这种情况下,您已经注册并登录,因此 firestore 安全规则不会入侵....快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.