VoidX
VoidX

Reputation: 13

Check if username is in use or not with realtime database flutter

I recently started using flutter, i love it. But i cant really understand how to retrieve data from firebase database, i mean, i do know how to do it with firestore but i wanna do it with the realtime database This is my case, i got a collection with the user names called 'names'. Whenever a user tries to create an account i check if the name he wants to use is availalable, so what i have to do is create a variable, that stores that name reference inside de 'names' collection if its null, then no one is using that name, otherwise, it is being used.

My implementation since now:

 var userNameRef = here i want to get the the name inside de 'names' collection ;

 //If the name is available
 if (userNameRef == null) {
   return true;
 }

 //That name already exists
 else {
   return false;
 }
} 

Upvotes: 0

Views: 602

Answers (1)

Gourango Sutradhar
Gourango Sutradhar

Reputation: 1619

Below function will do the work for you, please change the reference as your database structure:

bool getUserExistance()async{
        _databaseRef = FirebaseDatabase.instance.reference().child('users');
       dynamic result = await _databaseRef.child("users")
        .childOrderBy("name")
        .equals("your_name")
        .once();
         //If the name is available
         if (result == null) {
           return true;
         }
    
         //That name already exists
         else {
           return false;
         }
        } 

}

Upvotes: 1

Related Questions