String returning null value in android studio (java). I want to store the value from onComplete method to a string and use that outside that onComplete method. Please help me with the same..
final String[] sex = new String[1];
DocumentReference documentReference = firebaseFirestore.collection("users").document(mFirebaseAuth.getCurrentUser().getUid());
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
// Document found in the offline cache
DocumentSnapshot document = task.getResult();
sex[0] = document.getString("sex");
Log.d("", "Cached document data: " + document.getData());
} else {
Log.d("TAG", "Cached get failed: ", task.getException());
}
}
});
Toast.makeText(getContext(), "Gender :" + sex[0], Toast.LENGTH_SHORT).show();
Query query = null;
if (sex[0] == "Male") {
query = firebaseFirestore.collection("Female");
} else if (sex[0] == "Female") {
query = firebaseFirestore.collection("Male");
} else {
query = firebaseFirestore.collection("Others");
}