1

I have little experience with the firebase database. The following code doesn't work it as it doesn't retrieve the name of the user from the database. However, it shows the datasnapshot from the firebase. Here is the code.

databaseReference = FirebaseDatabase.getInstance().getReference().child("/userList/"+ FirebaseAuth.getInstance().getUid());
databaseReference.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        Log.d(TAG, "onChildAdded: " + dataSnapshot.toString());
        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
            Users users = dataSnapshot1.getValue(Users.class);
            String firstName = users.getFirstName();
            String lastName = users.getLastName();
            Log.d(TAG, "onChildAdded: " + firstName + " " + lastName);
        }
    }
}

The datasnapshot from firebase

onChildAdded: DataSnapshot { 
    key = -Lt_5ltPuMx6CPSk5X8-, 
    value = {
      firstName=firstname, 
      lastName=lastname, 
      email=firstname@gmail.com
    } 
}

Here's how I store the firebase data.

firebase data

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
gouri panda
  • 312
  • 2
  • 11

1 Answers1

3

The onChildAdded callback gets called for each child under /userList/$uid. Each time it gets called, the snapshot contains one of the child nodes. In the callback you are looping over the children of that snapshot, which means that your dataSnapshot1 refers to the individual properties (email, firstName, lastName). And since those are not full user nodes, the dataSnapshot1.getValue(Users.class) fails to read a user from them.

In short: remove the loop, and you should be getting better results:

databaseReference = FirebaseDatabase.getInstance().getReference().child("/userList/"+ FirebaseAuth.getInstance().getUid());
databaseReference.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        Log.d(TAG, "onChildAdded: " + dataSnapshot.toString());
        Users users = dataSnapshot.getValue(Users.class);
        String firstName = users.getFirstName();
        String lastName = users.getLastName();
        Log.d(TAG, "onChildAdded: " + firstName + " " + lastName);
    }
}

Also see these similar questions:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Good catch Frank! – Reaz Murshed Nov 13 '19 at 17:55
  • 1
    OK. So that sounds like your `Users` class does not a default/no-argument constructor. Be sure to add one (`public Users() {}`) and (as the message says) make sure Proguard does not strip methods from your data classes. If you search for the error message combined with Firebase you'll also get hits relevant to this problem: https://www.google.com/search?q=Users+does+not+define+a+no-argument+constructor+firebase – Frank van Puffelen Nov 13 '19 at 18:55
  • Thank you Frank. – gouri panda Nov 13 '19 at 19:07