使用实时数据库,可以做到这一点:
MyPojo pojo = dataSnapshot.getValue(MyPojo.Class);
作为一种映射对象的方法,如何使用 Firestore
来实现这一点?
代码:
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("app/users/" + uid).document("notifications").get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null) {
NotifPojo notifPojo = document....// here
return;
}
} else {
Log.d("FragNotif", "get failed with ", task.getException());
}
});
最佳答案
使用 DocumentSnapshot
您可以:
DocumentSnapshot document = future.get();
if (document.exists()) {
// convert document to POJO
NotifPojo notifPojo = document.toObject(NotifPojo.class);
}
关于java - Firebase Firestore : How to convert document object to a POJO on Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46639393/