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 document
variable, then how could I retrieve the value in that DocumentSnapshot
at the field "username"? The field has a string value.
Asked
Active
Viewed 9.3k times
38

Paradox
- 4,602
- 12
- 44
- 88
6 Answers
56
DocumentSnapshot has a method getString() which takes the name of a field and returns its value as a String.
String value = document.getString("username");

Doug Stevenson
- 297,357
- 32
- 422
- 441
-
1When I use this code, AndroidStudio does a red underline with `Incompatible types, Required: java.lang.String, found java.lang.Object` Why would it be giving a generic Object? – Paradox Jan 29 '18 at 00:59
-
You're probably getting a generic Object because the field doesn't have a String. Can you post a screenshot of your document in the Firestore console? – Rosário Pereira Fernandes Jan 29 '18 at 01:01
-
Updated with screenshot – Paradox Jan 29 '18 at 01:03
-
@Paradox use get string not get – Abdulrahman Abdelkader Jun 21 '21 at 15:47
18
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
11
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.

Steffo Dimfelt
- 870
- 12
- 11
2
I can only reference the field's data as Strings when I am inside the onComplete, but when I try reference it outside it. I get a nullPointerException and it crashes my activity.
// Gets user document from Firestore as reference
DocumentReference docRef = mFirestore.collection("users").document(userID);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
Log.d(TAG, "db firstName getString() is: " + document.getString("firstName"));
Log.d(TAG, "db lastName getString() is: " + document.getString("lastName"));
mFirstName = (String) document.getString("firstName");
mLastName = (String) document.getString("lastName");
Log.d(TAG, "String mFirstName is: " + mFirstName);
Log.d(TAG, "String mLastName is: " + mLastName);
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
//string checking outside the docRef.get().addOnCompleteListener code
//commented it out because it causes a java.lang.NullPointerException: println needs a message
//Log.v("NAME", mFirstName);
//Log.v("NAME", mLastName);
// sets the text on the TextViews
tvFirstName = (TextView)findViewById(R.id.tvFirstName);
tvFirstName.setText(mFirstName);
tvLastName = (TextView)findViewById(R.id.tvLastName);
tvLastName.setText(mLastName);

karldusenbery
- 21
- 3
2
Here is another simple way to get document value(in your case):
Firestore.instance
.collection('users').document('xsajAansjdna')
.get()
.then((value) =>
print("Fetched ==>>>"+value.data["username"]));

Jawad Ali
- 13,556
- 3
- 32
- 49

Muahmmad Tayyib
- 689
- 11
- 28
-
The point is that this is not performant, because you get whole the document, not just the desired field. – Jalaleddin Hosseini Jan 09 '22 at 19:27
1
`Firestore.instance
.collection('users').document('xsajAansjdna')
.get()
.then((value) =>
print("Fetched ==>>>"+value.data()["username"]));`

Sanat Kumar
- 82
- 5
-
2While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) will help people understand the reasons for your code suggestion. – Gerhard Mar 22 '22 at 07:31