0

I am working on a project on Android Studio with libGDX and I have connected the game to firebase which is working perfectly.

When I am trying to fetch some data, the firebase returns proper expected values, but the function is returning a null value.

public String fetchData(String id)
{
    reference = FirebaseDatabase.
            getInstance().getReference("Users").
            child(FirebaseAuth.getInstance().getCurrentUser().getUid());
    reference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            s = snapshot.child(id).getValue(String.class); //Here if we print s it gives the desired output.
        }

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

        }
    });

    return s; // But here the function is returning null.
}


@Override
    public String Background_Active() { 
        return fetchData("Background Active"); //Here I am calling the above function 
    }

If someone is aware of the solution to this issue, please do help me out.

  • There is no way you can do that. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Aug 17 '22 at 09:52

1 Answers1

0

The firebase onDataChange() is asynchronous you need to pass the value from inside onDataChange for example create a method like

onPostExecute(String s) 

Pass the data to this method from inside onDataChange() so your code would somewhat look like

public void fetchData(String id)
{
reference = FirebaseDatabase.
        
getInstance().getReference("Users").
        child(FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        s = snapshot.child(id).getValue(String.class); //Here if we print s it gives the desired output.
 onPostExecute(s);
    }

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

    }
});

   // return s; // But here the function is returning null.
}

For returning the value to the caller you can define a abstract inner class like

abstract GetValueListener{
abstract void onValueReceived(String s);
abstract void onErrorReceived(Throwable error);
}

Now modify fetchData method as following

public void fetchData(String id, GetValueListener listener)
{
reference = FirebaseDatabase.

getInstance().getReference("Users").
        child(FirebaseAuth.getInstance().getCurrentUser().getUid());
reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        s = snapshot.child(id).getValue(String.class); //Here if we 
print s it gives the desired output.
 listener.onValueReceived(s);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
 listener.onErrorReceived(error.toException());
    }
});

   // return s; // But here the function is returning null.
}

now when you call fetchdata your code should look like

fetchData(id, new GetValueListener(){
void onValueReceived(String s)
{
//value will be returned here
}

void onErrorReceived(Throwable error)
{
//if any error is received it will be returned here
}
});

Hope it helps. If for Libgdx this does not work please refer to this link https://libgdx.com/wiki/third-party/firebase-in-libgdx as i am not sure what is the requirement for Libgdx

  • Could you please explain how should I implement onPostExecute(s) as I need to return the value s, as I am working on libGDX? – Ashish Jain Aug 17 '22 at 06:23
  • If you just want to return the value to the caller then you can use a interface, i will edit the answer for it. I am not sure how libgdx works but i found this article on the web, https://libgdx.com/wiki/third-party/firebase-in-libgdx check if it helps. – Pradipto SenSarma Aug 18 '22 at 08:17