John
John

Reputation: 39

Whys isn't Firebase Data snapshot returning any values (Android)?

I'm having trouble getting any values from my firebase database for android using the documentation that was given. I've tried connecting to the simplest structure possible but still no luck. Below is my code.

DatabaseReference usersReference;

@Override
protected void onCreate(Bundle savedInstanceState) {
...
    usersReference = FirebaseDatabase.getInstance().getReference("users");
...
}


protected void onStart() {
    super.onStart();

    System.out.println("User Reference: "+usersReference.toString());
    usersReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String value = dataSnapshot.child("name").getValue(String.class);
            mongo.setText(value);
        }

        @Override
        public void onCancelled(DatabaseError error) {

        }
    });
}

Here is what my database looks like in firebase

enter image description here

For some reason no matter what I try I get nothing. I tried printing the user reference to make sure I was going to the correct place, and although the link was the correct reference to the database when I clicked the link the I was met with this screen: enter image description here

Not sure if that is relevant or not. Thank you

Upvotes: 0

Views: 87

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

From the rest of the behavior you're describing it seems like your code is looking at a different database from what's shown in the screenshot. I recommend re-downloading the google-services.json for your project, and adding it to the project again.

Upvotes: 1

Codigo Morsa
Codigo Morsa

Reputation: 840

When that happens, you better use the debugger to understand what's going on. Are you using Android Studio right?

Put a breakpoint on the following line:

String value = dataSnapshot.child("name").getValue(String.class);

And then, when the program execution stops here, click the "evaluate expression" button. Inside that window you can experiment with dataSnapshot. I suggest you to try dataSnapshot.getChildren() there and see what does it show.

Upvotes: 0

Related Questions