Problem with Data Reading in Firebase

Asked

Viewed 44 times

0

Hello, I’m trying to read data in firebase and pass to an object, but when I used to use this object outside the onDataChange it showed the object data as null

So I made the following Log’s to test: inserir a descrição da imagem aqui

public void readDatas(){

    valueEventListener = ref.child("users").child(userId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            user = dataSnapshot.getValue(User.class);
            Log.i("test", "id 1 "+user.getCurrentCharacter());

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
    Log.i("test", "id 2 "+user.getCurrentCharacter());
}

And it seems that this happened because it has a delay when reading the Firebase data and passing to the object.

How could I solve this problem?

Thank you in advance

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.