java - Firebase Firestore : How to convert document object to a POJO on Android

标签 java android firebase google-cloud-firestore

使用实时数据库,可以做到这一点:

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/

相关文章:

java - Jnotify 是如何工作的

android - 如何从 Genymotion 设备获取 LogCat

java - Android 客户端-服务器应用程序 - readLine() 不起作用

firebase - 如何将图标上传到Firebase存储

angular - 如何更改 Cloud Firestore 中数组内的对象?

firebase - 使用 Firebase 身份验证的 SwiftUI

java - 如何使用 JSON 序列化/反序列化 Option<> 类(函数式 java)?

java - WAS Liberty Profile 连接到 MQ 服务器

java - 每次查看 Activity 时如何调用方法?

android - ActivityResult、Facebook sdk 的 NullPointerException