Firestore get DocumentSnapshot's field's value
DocumentSnapshot has a method getString() which takes the name of a field and returns its value as a String.
String value = document.getString("username");
you can use get
method to get value of a field
String username = (String) document.get("username"); //if the field is String
Boolean b = (Boolean) document.get("isPublic"); //if the field is Boolean
Integer i = (Integer) document.get("age") //if the field is Integer
checkout the doc for DocumentSnapshot
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<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> 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.