I am planning to use the value of a sub-field of a map field in my document for other purposes but it seems that I cannot retrieve it. I have found an answer here on this website but the solution code to get the value is too much for me. I can use the solution code to get the value but if there is the simplest way to get it, kindly drop the answer here.
This is the screenshot of Firestore DB where I need to get is the Boolean value of deleted inside a map field with the UID as field name:
CodePudding user response:
To get the value of the "deleted" fields that exists inside that Map object, please use the following lines of code:
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference candidatesRef = db.collection("Candidates");
DocumentReference emailRef = candidatesRef.document("[email protected]");
// ^ add entire address ^
emailRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Map<String, Boolean> map = (Map<String, Boolean>) document.get("PADc...ayU2");
// ^ add entire ID
boolean deleted = map.get("deleted");
Log.d(TAG, "deleted: " deleted);
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});
The result in the logcat will be:
deleted: true