I am trying to retrieve data from the firebase realtime database and save it to a variable like this:
mUserDatabase.child(mCurrentUserId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
myPublicKeyString = dataSnapshot.child("public_key").getValue().toString();
myPrivateKeyString = dataSnapshot.child("private_key").getValue().toString();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Where myPublicKeyString
and myPrivateKeyString
are global variables. When I try to do a System.out.println(myPublicKeyString);
inside the ValueEventListener
function it does indeed print out the correct data but when I try to do the same outside of the function it prints out null
.