问题
Hello I'm trying to retrieve the data from a realtime firebase field but it returns null. I checked with textView52.setText (firebaseUser.getEmail ()); to see if it was really connected and it works. the problem is in recovering a value from the database which in this case is the field "name" could someone help me? am i doing something wrong? any help is welcome.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userUid = user.getUid();
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String data = dataSnapshot.child("name").getValue(String.class);
textView52.setText(data);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
回答1:
try
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userUid = user.getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.child("Users").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String data = dataSnapshot.child("name").getValue();
textView52.setText(data);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});}
回答2:
UPDATED! check those lines
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Users");
ref.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {...}
Basically you try to call in Users another child called User, to understand very easy, replace the of that user here
ref.child("use_that_key_here").addListenerForSingleValueEvent(new ValueEventListener() {...}
Is not a solution, you cannot hard code every key, but with this you should see the data
try something like that:
// ref to db
DatabaseReference ref= FirebaseDatabase.getInstance();
ref.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//debug this par and check if you can see your user
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
来源:https://stackoverflow.com/questions/60050114/retrieve-data-currentuser-from-firebase-return-null