Hi I have a Kotlin data class as follows

data class User (
        @get:Exclude val gUser: Boolean,
        @get:Exclude val uid: String,
        @get:PropertyName("display_name") val displayName: String,
        @get:PropertyName("email") val email: String,
        @get:PropertyName("account_picture_url") val accountPicUrl: String,
        @get:PropertyName("provider") val provider: String
)

我能够序列化对象而不会出现问题.但在执行firebase查询时,我无法反序列化对象.目前我正在做的就是获取数据

_firebaseReference.child(getString(R.string.firebase_users_key)).child(user.uid)
        .setValue(user).addOnCompleteListener{
    _firebaseReference.child("users").child(user.uid)
            .addListenerForSingleValueEvent(object : ValueEventListener {
        override fun onCancelled(p0: DatabaseError) {

        }

        override fun onDataChange(p0: DataSnapshot) {
            if (p0.exists()) {
                val userHash = p0.value as HashMap<*, *>
                var currentUser: User
                if (userHash[getString(R.string.provider_key)]
                        != getString(R.string.provider_google)) {
                    currentUser = User(false, p0.key!!, 
                            userHash["display_name"].toString(), 
                            userHash["email"].toString(),
                            userHash["account_picture_url"].toString(), 
                            userHash["provider"].toString())
                } else {
                    currentUser = User(true, p0.key!!, 
                            userHash["display_name"].toString(), 
                            userHash["email"].toString(), 
                            userHash["account_picture_url"].toString(), 
                            userHash["provider"].toString())
                }
            }
        }

    })
}

This is only a test project that i'm working on to practice my Kotlin, but this is something I would like to figure out.

如果我做错了,请告诉我,任何建议都将不胜感激

谢谢

推荐答案

Firebase需要一个空构造函数才能反序列化对象:

data class User(
        @Exclude val gUser: Boolean,
        @Exclude val uid: String,
        @PropertyName("display_name") val displayName: String,
        @PropertyName("email") val email: String,
        @PropertyName("account_picture_url") val accountPicUrl: String,
        @PropertyName("provider") val provider: String
) {
    constructor() : this(false, "", "", "", "", "")
}

您可以这样声明,并提供一些默认值,以便能够调用主构造函数,也可以为所有参数声明默认值:

data class User (
        @Exclude val gUser: Boolean = false,
        @Exclude val uid: String = "",
        @PropertyName("display_name") val displayName: String = "",
        @PropertyName("email") val email: String = "",
        @PropertyName("account_picture_url") val accountPicUrl: String = "",
        @PropertyName("provider") val provider: String = ""
)

Then various constructors will be created for you, including an empty constructor.

如果序列化有问题,可能是因为ide生成的getter和setter,请try 使用@get和@set注释来加强它们:

data class User (
        @Exclude val gUser: Boolean = false,
        @Exclude val uid: String = "",
        @set:PropertyName("display_name") 
        @get:PropertyName("display_name")
        var displayName: String = "",
        @PropertyName("email") val email: String = "",
        @set:PropertyName("account_picture_url")
        @get:PropertyName("account_picture_url")
        var accountPicUrl: String = "",
        @PropertyName("provider") val provider: String = ""
)

Kotlin相关问答推荐

try 一次性插入多条记录时,JOOQ连接为空错误

Kotlin中是否可以混合使用推断和显式的通用类型参数?

使用 Kotlin 的 Springboot 中缺少 ResponseEntity 正文属性

Kotlin 中的 maxOf() 和 max() 方法有什么区别?

如何在 Spring Boot 3 中为内部类提供运行时提示

有什么方法可以要求在 kotlin 中的类型参数上进行注释?

有什么方法可以要求在 kotlin 中的类型参数上进行注释?

使用事务时未调用 Kafka ConsumerInterceptor onCommit

kotest 更改环境变量

Mixin 在 Jackson 中添加 defaultImpl 不起作用

Jetpack compose 可滚动表格

如何在 Kotlin 中使用具有继承的泛型

Kotlin 中多个 init 块的用例?

将 Gradle 子元素与 Kotlin 多平台一起使用

如何将 kotlin 集合作为 varagrs 传递?

Kotlin val difference getter override vs assignment

如何解决:将Java类转换为Kotlin后出现error: cannot find symbol class ...?

在 Kotlin 中创建 Spinner 时,如何在 Fragment 中的旋转屏幕上修复指定为非空的参数为空?

用于代码生成的ANTLR工具版本4.7.1与当前运行时版本4.5.3不匹配

Gradle:无法连接到 Windows 上的 Kotlin 守护程序