I have a Flutter app with a "Create Account" form that has a field for Username. Upon entering a username and hitting the "Submit" button, my validator functions run for the form. I would like to query my Firestore database and ensure that the username entered isn't already taken.
My validator
(in the Username form):
validator: (value) {
Future<bool> usernameTaken = uniqueCheck(value);
if (usernameTaken) {
return 'Username is already taken!';
} else {
return null;
}
My uniqueCheck
function:
Future<bool> uniqueCheck(String? value) async {
bool alreadyTaken = false;
var snapshot = await FirebaseFirestore.instance
.collection('users')
.where('username', isEqualTo: value)
.get();
snapshot.docs.forEach((result) {
alreadyTaken = true;
});
return alreadyTaken;
}
The trouble is my validator
finishes before my uniqueCheck
function can return with the proper result (such as if the username is taken and usernameTaken
is true
. I thought await
would fix this, but apparently not.
What am I doing wrong here?