4

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
  • checkUser() returns Future<bool> Commented Nov 1, 2018 at 15:50

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much for your help Zroq. The problem is that the validator as written above is throwing an error "The return type bool isn't a String as defined by anonymous closure". Any idea what is causing this?
I actually think my question is how are you initializing the userNameValidator? After the CheckUser function, how is that set to true or false?
You should initialize the variable to false after the State subclass is defined. Like the _isFavorited variable in this example - flutter.io/tutorials/interactive/#step-3
Thanks for this, I realized I wasn't using the controller properly to validate the text. Still feeling like a novice with this stuff, but appreciate your help greatly!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.