我似乎无法弄清楚如何将从Firestore检索的数据存储在自定义对象类型的全局变量中。我可以从.addOnSuccessListener
中打印出数据,但不能将数据分配给全局变量。
我的代码如下:
override fun getData(documentPath: String): ShopModel {
var shopModel = ShopModel()
firestore.firestoreSettings = FirebaseFirestoreSettings.Builder().build()
val document = firestore.collection("shops").document(documentPath)
document.get().addOnSuccessListener {
shopModel = it.toObject(ShopModel::class.java)!!
info(shopModel) //this prints the model out and it works
}
return shopModel //this returns an object with empty fields
}