The privacy field in your document can be considered Map<String, Boolean> , so you can specify the value of this field in such a variable:
HashMap<String, Boolean> privacy = (HashMap<String, Boolean>) task.getResult().getData().get("privacy");
Now the main problem is that you will most likely see a compiler βuntested throwβ warning, because casting a Map like this is not ideal, since you cannot guarantee that the database structure will always contain String : Boolean values in this field.
In this case, I would suggest using custom objects for storage and retrieve objects in your database that will automatically handle marching and casting for you:
DocumentReference docRef = FirebaseFirestore.getInstance().collection("Users").document("PQ8QUHno6QdPwM89DsVTItrHGWJ3"); docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (document != null) { User user = task.getResult().toObject(User.class); } else { Log.d(TAG, "No such document"); } } else { Log.d(TAG, "get failed with ", task.getException()); } } });
If your User class matters:
public class User { private String username; private HashMap<String, Boolean> privacy; public User() {} public User(String username, HashMap<String, Boolean> privacy) { this.username = username; this.privacy = privacy; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public HashMap<String, Boolean> getPrivacy() { return username; } public void setPrivacy(HashMap<String, Boolean> privacy) { this.privacy = privacy; } }
In this example, calling User user = task.getResult().toObject(User.class) will march the entire document into an instance of your User object, and then you can access the privacy map with:
HashMap<String, Boolean> userPrivacy = user.getPrivacy();
Each field in the document will be mapped to a field with the same name inside your custom object, so you can also add settings or photo_url in the same way. You just need to remember:
Each custom class must have an open constructor that takes no arguments. In addition, the class must include a public getter for each property.