Reputation: 179
I have this db:
root
\_ users
\_ uid
\_ name: "Pathis"
\_ admin: true
\_ uid
\_ name: "Venkat"
\_ admin: false
Code to get the data:
db.collection("users").whereEqualTo("admin", true).get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
boolean admin = document.getBoolean("admin");
}
}
});
It works because I know the types (name is a String, admin is a boolean). But how would this work if I did not know them? What I need is a way to get the document and verify each property. Let's say I find name and I would like to verify as String type, I find admin and I should get boolean. Is there a solution for this?
Upvotes: 0
Views: 180
Reputation: 317712
If you don't know the type of value ahead of time, you will have to access the value as an Object using get(), then check its type using the Java instanceof
operator. For example:
Object adminObject = document.get("admin");
if (adminObject instanceof String) {
String admin = (String) adminObject;
}
else if (adminObject instanceof Boolean) {
boolean admin = (Boolean) adminObject;
}
This is not an exhaustive approach - just a short example. You might need to handle many other types, and your code might need to adapt.
Upvotes: 1