如何解析Firestore到Kotlin的时间戳



我在尝试从Firestore解析时间戳时得到此错误。

运行应用程序时出错

data class User(
val id: String,
val name: String,
val img: String,
val birth: Timestamp? = null
){
constructor() : this (
"",
"",
"",

)
}

我想解析Firestore日期以显示用户的生日。

如果您需要使用类型为User的对象添加时间戳类型字段,那么您必须在类中定义类型为Date的字段,并使用如下注释:

@ServerTimestamp
var birth: Date? = null
//          👆

创建User类的对象时,不需要设置日期。您不必向构造函数传递任何东西。Firebase服务器将读取birth字段,因为它是一个ServerTimestamp(请参阅注释),并且它将相应地填充该字段。

否则,创建一个Map并放置:

"birth" to FieldValue.serverTimestamp()

最新更新