I wrote a function that is supposed to get a user id, access all the user's attributes in the firestore database, and then create and return the object. But the function skips over the entire block of "addOnSuccessListener", so it returns a null object.
public static Player getPlayer(String playerID) {
final Player[] p = new Player[1];
DocumentReference playerRef = fStore.collection("users").document(playerID);
playerRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
// getting all the attributes of the player from the database
String age = documentSnapshot.getString("age");
String city = documentSnapshot.getString("city");
String email = documentSnapshot.getString("email");
String fullName = documentSnapshot.getString("fullName");
String gender = documentSnapshot.getString("gender");
String nickName = documentSnapshot.getString("nickName");
String password = documentSnapshot.getString("password");
String phone = documentSnapshot.getString("phone");
// creates a new player
p[0] = new Player(fullName, nickName, email, password, phone, city, gender, age);
} else {
Toast.makeText(context.getApplicationContext(), "Player not found", Toast.LENGTH_LONG).show();
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context.getApplicationContext(), "Failed to fetch data", Toast.LENGTH_LONG).show();
}
});
return p[0];
}