Firestore get DocumentSnapshot's field's value

后端 未结 5 579
盖世英雄少女心
盖世英雄少女心 2021-02-02 09:10

If I have a Firebase Firestore database which I have retrieved a DocumentSnapshot for the document corresponding to the collection on the right and stored in a

5条回答
  •  日久生厌
    2021-02-02 09:51

    You need to do a DocumentReference to get the content in your document.

    A simple one will be like this.

    DocumentReference docRef = myDB.collection("users").document("username");
    docRef.get().addOnCompleteListener(new OnCompleteListener() {
    @Override
         public void onComplete(@NonNull Task task) {
              if (task.isSuccessful()) {
                   DocumentSnapshot document = task.getResult();
                        if (document != null) {
                             Log.i("LOGGER","First "+document.getString("first"));
                             Log.i("LOGGER","Last "+document.getString("last"));
                             Log.i("LOGGER","Born "+document.getString("born"));
                        } else {
                             Log.d("LOGGER", "No such document");
                        }
                   } else {
                        Log.d("LOGGER", "get failed with ", task.getException());
                    }
              }
         });
    

    The downside is that you need to know your document ID to get the field values.

提交回复
热议问题