java Firebase Firestore:如何在Android上将文档对象转换为POJO
使用实时数据库,您可以执行以下操作:
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());
}
});
# 1 楼答案
不确定这是否是最好的方法,但这就是我目前所拥有的
编辑:我现在正在使用已接受答案上的内容
# 2 楼答案
Java
Kotlin
请务必记住,必须提供默认构造函数,否则将出现经典的反序列化错误。对于Javaa
Notif() {}
就足够了。对于Kotlin初始化属性# 3 楼答案
使用
DocumentSnapshot
可以执行以下操作: