Tag: google cloud firestore

如何使用Kotlin反序列化包含DocumentReference的Firestore DocumentSnapshot

我有以下Firestore结构: 我设法使用Kotlin反序列化它,但只使用Any作为城市和活动的类型。 到目前为止,这是我的数据类: @IgnoreExtraProperties data class User ( var firstName: String, var lastName: String, var displayName: String, var birthday: Date, var memberSince: Date, var city: Any, var activities: ArrayList<Any> ) { constructor():this("", "", "", Date(), Date(), Any(), ArrayList(0)) } 这里是我如何检索数据和反序列化: DocumentReference userRef = mFirestoreDB.collection("user").document(mFirebaseUser.getUid()); userRef.addSnapshotListener(this, new EventListener<DocumentSnapshot>() { @Override public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) […]

Kotlin – 如何做onCompleteListener从Firestore获取数据?

我有一个问题从Firestore中获取数据,在JavaCode中我们可以这样做: DocumentReference docRef = db.collection("cities").document("SF"); docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { @Override public void onComplete(@NonNull Task<DocumentSnapshot> task) { if (task.isSuccessful()) { DocumentSnapshot document = task.getResult(); if (document != null) { Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData()); } else { Log.d(TAG, "No such document"); } } else { Log.d(TAG, "get failed with ", task.getException()); } } }); 但在Kotlin,当我尝试覆盖onComplete函数,它是不可用的。 那么,我怎样才能得到“任务”呢?

使用Kotlin Firebase Firestore toObject()

我尝试在Kotlin项目中使用Firebase Firestore。 除了当我想用DocumentSnapshot.toObject(Class valueType)实例化对象时,一切都很好。 这里是代码: FirebaseFirestore .getInstance() .collection("myObjects") .addSnapshotListener(this, { querySnapshot: QuerySnapshot?, e: FirebaseFirestoreException? -> for (document in querySnapshot.documents) { val myObject = document.toObject(MyObject::class.java) Log.e(TAG,document.data.get("foo")) // Print : "foo" Log.e(TAG, myObject.foo) // Print : "" } } }) 正如你所看到的,当我使用documentChange.document.toObject(MyObject::class.java) ,我的对象被实例化,但内部字段没有设置。 我知道Firestore需要模型有一个空的构造函数。 所以这里是模型: class MyObject { var foo: String = "" constructor(){} } 有人能告诉我我做错了什么吗? 谢谢