Skip to content
Advertisement

Firebase Datasnapshot returns null value

I want to retrieve this value from this node (“id”), and the value i get is null. I have googled so many solutions that this might have to do with asynchronous way or something, i guess?

This is the database, and the highlighted node is the value i would like to get:

This is the database, and the highlighted node is the value i would like to get

This is my code:

JavaScript

Most appreciated if someone can help me out of this!

Advertisement

Answer

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

Your onDataChange needs to handle this list by looping over dataSnapshot.getChildren()):

JavaScript

A few more notes:

  • Any use of id needs to happen inside onDataChange, or be called from there. Outside of that, you won’t have any guarantees that id will have been assigned the value you expect.
  • Using toasts for debugging is bound to become confusing. I highly recommend using Log.d(...) and friends, and studying the output (and its order) in the logcat output of your app.
Advertisement