Ad

How To Get Data From Firestore Document

- 1 answer

I have a collection called 'Skate' is it possible to get all data from her document? for example as in the photo the data of the two parks

fireStore.collection("Parks").document("PracasGravatai").collection("Skate").document().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if(documentSnapshot.exists()) {
                        String endereco = documentSnapshot.getString("Endereco");
                        String nome = documentSnapshot.getString("Nome");
                        Log.i("LOG: ",endereco);
                        Log.i("LOG",nome);

                        textView.setText("Nome: "+nome+ "Endereço: "+endereco);
Ad

Answer

A collection contains documents. Right now you call document(), which means that you're creating a reference to a new, empty document. So the get() call after that then returns a snapshot to a non-existing document.

To load a specific document, you'll need to know its ID. Once you do, you can load it by calling get on the DocumentReference:

fireStore.collection("Parks").document("PracasGravatai")
         .collection("Skate").document("theIdOfTheDocument").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {

If you want to load all documents in the Skate collection for PracasGravatai, you can do so by calling get on the collection:

fireStore.collection("Parks").document("PracasGravatai")
         .collection("Skate").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                Log.d(TAG, document.getId() + " => " + document.getData());
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

Both of these cases are covered in the Firebase documentation on getting data from Firestore.

Ad
source: stackoverflow.com
Ad