How do I check if specific child value exists in FireBase (Android)


I have some trouble trying to check if user information is stored already in the FireBase database. Basically I'm trying to do something stupid like this: "select user_name from user where user_id="+userID+" And if the nickname exists it should make the boolean var isFirstTime = false and if it doesn't it should stay true. And after that it should show register box or not.

This is my db: <a href="https://i.stack.imgur.com/25tNt.png" rel="nofollow">Firebase</a> And this is my code in onCreate method:

databaseReference = FirebaseDatabase.getInstance().getReference(); DatabaseReference dbRefFirstTimeCheck = databaseReference.child("User").child(user.getUid()).child("Nickname"); isFirstTime = true; dbRefFirstTimeCheck.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot.getValue() != null) { isFirstTime=false; } } @Override public void onCancelled(DatabaseError databaseError) { } }); if(isFirstTime) { showNewUserBox(); } else { }

No matter what I do, the methor showNewUserBox() is being called. How do I get the data i need and check if it's there?


As others have commented, data is loaded from Firebase asynchronously. By the time you check isFirstTime, the data hasn't been loaded yet, onDataChange hasn't been run yet, so ifFirstTime will have its default value (false for a boolean).

All code that requires data from the database should be inside onDataChange (or invoked from within there). The simplest fix for your code is:

databaseReference = FirebaseDatabase.getInstance().getReference(); DatabaseReference dbRefFirstTimeCheck = databaseReference.child("User").child(user.getUid()).child("Nickname"); dbRefFirstTimeCheck.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot.exists()) { showNewUserBox(); } } @Override public void onCancelled(DatabaseError databaseError) { throw databaseError.toException(); // don't ignore errors } });

Also see some of the many <a href="https://stackoverflow.com/search?tab=votes&q=%5bfirebase-database%5d%5bandroid%5d%20asynchronous" rel="nofollow">questions about asynchronous loading from Firebase</a>, such as <a href="https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519" rel="nofollow">getContactsFromFirebase() method return an empty list</a> (or this quite old classic: <a href="https://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener/33204705#33204705" rel="nofollow">Setting Singleton property value in Firebase Listener</a>).


  • Firebase Android method to check existence of user's profile info in Firebase database
  • Why doesn't the firebase query look like list?
  • How can I get some value from my Firebase?
  • Firebase Database select by inner field
  • How to retrieve data from firebase to google map?
  • Firebase always returning all the rows
  • com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String t
  • Listen to multiple database references [duplicate]
  • Update Marker when Location Change based on Value of Child Firebase Android
  • Populatate the spinner from Firebase database
  • Is Android's ARGB_8888 Bitmap internal format always RGBA?
  • OnItemSelectedListener for Spinner that was added programmatically does not trigger
  • DependencyObject.AssociatedObject is always null
  • Curried UDF - Pyspark
  • Replace Fragment with another on back button
  • Avoid registering duplicate broadcast receivers in Android
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Django Haystack Rebuild Index
  • Magento get URL before current
  • How to get listview position?
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Keep this build forever option - Jenkins
  • Installing iPhone App to iPhone
  • Spring security and special characters
  • javascript inside java/jsp code
  • Sending data from AppleScript to FileMaker records
  • Trying to switch camera back to front but getting exception
  • How to show dropdown in excel using jrxml (jasper api)?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • Acquiring multiple attributes from .xml file in c#
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Programmatically clearing map cache
  • java string with new operator and a literal