0

I try to make arraylist from the firestore. When I run the code the app got crashes. Do I need to add something here to make the app run? And how can I get all the document in firestore that have unique id?

public String load() {

    contentList = new ArrayList<>();
   
    contentList.clear();
    firebaseFirestore.collection("plans").document("W1ls1b0lYNw9hxyuJyJ5").collection("versions").get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {

                        for (QueryDocumentSnapshot doc : task.getResult()) {

                                String List = doc.getString("content");
                                contentList.add(List);





                        }
                        Log.d(TAG, "Length is:"+ " => " + contentList.size());
                        Log.d(TAG, "a : =>" + contentList.get(0));
                        Log.d(TAG, "b : =>" + contentList.get(1));
                        Log.d(TAG, "c : =>" + contentList.get(2));
                        Log.d(TAG, "d : =>" + contentList.get(3));


                    }else {
                        Log.e(TAG, "Firestore error");
                    }
                }

            });

    return contentList.get(0).toString();
    //contentList.get(0).toString()
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
falc0n027
  • 1
  • 1
  • Can you share the logcat? – Sujal Kumar Jan 13 '22 at 08:15
  • 1
    There is no way you can call `contentList.get(0)` outside the callback. 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 13 '22 at 09:31

0 Answers0