0
package com.boomtech.tremendoustrivia;

import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

public class DocumentName {
    FirebaseFirestore firestore= FirebaseFirestore.getInstance();

   public List<String> getDomumentName() {
        List<String> docunames=new ArrayList<>();
        firestore.collection("Quizquestions").get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        List<DocumentSnapshot> snapshotList = queryDocumentSnapshots.getDocuments();
                        for (DocumentSnapshot snapshot:snapshotList){
                            docunames.add(snapshot.getId());
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                    }
                });
        return docunames;
    }
}

Why this code is returning null? It's not returning the actual List, it's just returning a Null value. How can I get the document's name and pass it to the string of the list so that I can get it whenever I want.

Nathan
  • 73,987
  • 14
  • 40
  • 69
  • You will find your answer here: https://stackoverflow.com/a/48766014/19144006 – Abu bakar Jun 23 '22 at 05:40
  • 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-cloud-firestore-using-get-bf03b6ee4953). – Alex Mamo Jun 23 '22 at 07:56
  • Check out [this question](https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-or-launches-a-coroutine-return-an-empty-o) too. – Tyler V Jun 25 '22 at 02:15

1 Answers1

1

You need to wait before sanding results back for this use interface

public class DocumentName {
public DocumentName() {
}

FirebaseFirestore firestore= FirebaseFirestore.getInstance();

public void getDomumentName(FirebaseResults firebaseResults) {
    firestore.collection("Quizquestions").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    ArrayList<String> docunames=new ArrayList<>();
                    List<DocumentSnapshot> snapshotList = task.getResult().getDocuments();
                    for (DocumentSnapshot snapshot:snapshotList){
                        docunames.add(snapshot.getId());
                    }
                    firebaseResults.AllDocuments(docunames);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

                }
            });
}

interface FirebaseResults{
    void AllDocuments(ArrayList<String> names);
}

}

and Call it like this

new DocumentName().getDomumentName(new DocumentName.FirebaseResults() {
        @Override
        public void AllDocuments(ArrayList<String> names) {
            
        }
    });