0

So i have the email of all the acconts, that exists in my firestore users ("Registos") collection in a spinner, and whenever i click the email i want, i need to get the uid, So i wanted to create a function that returns the uid by email whenever i need to. Here is what i have :

public String GetId(String email) {
    String Id = "";
    mFirestore.collection("Registos")
            .whereEqualTo("Email", email)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            Id = (document.getId());
                            getUserID(Id);
                            Log.d("TAG1", "Id " + userID);
                            Log.d("TAG2", document.getId() + " => " + document.getData());
                        }
                    } else {
                        Log.d("TAG3", "Error getting documents: ", task.getException());
                    }
                }
            });
    return Id;
}

So first i get an error in Id inside the for loop : Variable 'Id' is accessed from within inner class, needs to be final or effectively final

And even if i make the suggested changes, this function allways return null instead of returning the value that "document.getId()" gets, which is correct.

Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
  • 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 article, [How to read data from Cloud Firestore using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Jan 20 '22 at 11:23

0 Answers0