Home > database >  How to get key from QuerySnapshot in android firestore
How to get key from QuerySnapshot in android firestore

Time:02-06

Here is an image of my database.

Above is an image of my database.The three fields have other info linked to them.

I want to store the names of these three documents (Joke Writing, Negotiation, Psychology) in a list. Here is what I'm doing:

List<String> na = new ArrayList<>();
        db.collection("courses").get().addOnCompleteListener(task -> {
            for(DocumentSnapshot document:task.getResult().getDocuments()){
                    na.add((String) document.get("key"));
            }
            Log.i("TAG", String.valueOf(na));
        });

However, na is printing [null,null,null]. How can I get the keys?

CodePudding user response:

Your code reads a field called key from each document, and it looks like no such field exits.

If you instead want to get the document ID for each document, that'd be:

na.add(document.getId());

I recommend keeping the reference documentation handy for this type of problem.

  •  Tags:  
  • Related