如何通过自定义对象从 Firestore 获取文档内容?(Android - Kotlin)

Ali*_*bah 5 android kotlin google-cloud-firestore

我正在尝试从 Firestore 获取文档内容。下图链接显示了数据库结构Firestore 数据库结构

我想要什么:我想通过自定义对象获取文档内容并将内容添加到列表中。

问题:我收到此错误: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.aalmesbah.turoodpilot, PID: 12160 java.lang.RuntimeException: 无法反序列化对象。com.google.firebase.auth.UserInfo 类未定义无参数构造函数。如果您使用 ProGuard,请确保这些构造函数没有被剥离

我试图通过get() 和 getString()方法获取文档内容并且它工作正常问题仅在于toObject()?

我已经搜索并尝试了一些来自其他问题的建议解决方案,例如为数据类添加默认值,但不幸的是它没有用。

数据类代码:

data class UserInfo (val name: String? = "",
                 val email: String? = "",
                 val phoneNum: String? = "",
                 val address: String? = "") {
    constructor(): this("","","", "" )
Run Code Online (Sandbox Code Playgroud)

}

配置文件片段代码:(假设显示文档内容的地方)

class ProfileFragment : Fragment() {

private lateinit var auth: FirebaseAuth
private lateinit var db: FirebaseFirestore


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    val view = inflater.inflate(R.layout.fragment_profile, container, false)

    auth = FirebaseAuth.getInstance()
    db = FirebaseFirestore.getInstance()

    return view
}

override fun onStart() {
    super.onStart()

    val userID = auth.currentUser?.uid
    val docRef = db.collection("users").document(userID!!)

    docRef.addSnapshotListener(EventListener<DocumentSnapshot> { documentSnapshot, e ->

    if (e != null) {
            Log.w(TAG, "Listen failed.", e)
            return@EventListener
        }

        if (documentSnapshot != null && documentSnapshot.exists()) {

            docRef.get().addOnSuccessListener { documentSnapshot ->
                val userInfo = documentSnapshot.toObject(UserInfo::class.java)

                emailTV.text = userInfo?.email
            }
        } else {

                Log.d(TAG, "Current data: null")
            }

        })


}
Run Code Online (Sandbox Code Playgroud)

}

注册活动中的 sendUserData() 方法代码

private fun sendUserData() {

    val name = userN.text.toString()
    val email = userEm.text.toString()
    val phone = userPhone.text.toString()
    val addressName = addressName.text.toString()
    val area = area.text.toString()
    val block = block.text.toString()
    val street = strees.text.toString()
    val avenue = avenue.text.toString()
    val house = house.text.toString()
    val floor = floor.text.toString()
    val apartment = apartment.text.toString()
    val additionalInfo = additional.text.toString()

    val address = "Addres Name: $addressName \n Area: $area \n B: $block ST: $street Av: $avenue H: $house\n " +
            "Floor: $floor Apartment: $apartment \n Notes: $additionalInfo"

    val userID = auth.currentUser?.uid

    val userData = UserInfo(name, email, phone, address)

    db.collection("users").document(userID!!).set(userData).addOnSuccessListener {
            Toast.makeText(this, "Successfully Registered", Toast.LENGTH_SHORT).show()
        }.addOnFailureListener{
        Toast.makeText(this, "Data Upload error!", Toast.LENGTH_SHORT).show()
        }


}    
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 3

如果您想将 Kotlin 数据类与 一起使用documentSnapshot.toObject,则必须将每个字段设置为可为空var而不是val。Firestore SDK 不知道如何将文档字段映射到数据类构造函数参数中。

如果您想要一个带有val字段的适当的不可变数据类,您将必须手动从文档中读取每个字段,并自己调用数据类构造函数。