Flutter async form validation

时间秒杀一切 提交于 2021-02-16 20:04:58

问题


I am having the same problem as the poster in this question: flutter validate form asynchronously.

However, I'm having trouble implementing this and understand how the validation is working.

First, what does this code return? Does this return a boolean?

Future checkUser() async {
var user = await Firestore.instance
    .collection('users')
    .document(userNameController.text)
    .get();
return user.exists;

Second, what is the userNameValidator in this case? Is this a boolean?

new TextFormField(
              validator: (value) {
                return usernameValidator;
              },

Thanks, for the help.


回答1:


The method checkUser will begin a call to Firebase. The await keyword is telling the compiler that we need the output of that call, but there is no need to block everything else from running. After the call has been made, the function will return a bool indicating if the given user exists.

On the other question checkUser is called via the onPressed call which waits for a return value and assigns it to the usernameValidator variable - therefore making it a bool.



来源:https://stackoverflow.com/questions/53104710/flutter-async-form-validation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!