【发布时间】:2018-03-19 21:26:49
【问题描述】:
我尝试在 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(){}
}
谁能告诉我我做错了什么?
谢谢
【问题讨论】:
标签: kotlin google-cloud-firestore