【问题标题】:Why does Kotlin data class type change a value name when writing to a Firestore Document?为什么 Kotlin 数据类类型在写入 Firestore 文档时会更改值名称?
【发布时间】:2020-11-16 09:12:57
【问题描述】:

我在将 Kotlin 数据类对象写入云 Firestore 上的文档时遇到了问题。在我假设这是某种错误之前,我想问一下这是否是预期的行为。

我有一个类似的数据类

data class Notification(
    val createdAt: Timestamp?,
    val isDismissed: Boolean?,
    val text: String?
)

我直接将它添加到 Firestore,例如:

docRef?.add(notification)

但是写入 Firestore 控制台的文档是这样的:

    createdAt: month number, 2020 at time AM/PM UTC+8
    dismissed: false
    text: "My text."

在写入 Firestore 时,是什么导致布尔值从 isDismissed 变为 dismissed

【问题讨论】:

    标签: firebase kotlin google-cloud-firestore boolean


    【解决方案1】:

    这与 JavaBeans 访问器方法的命名约定有关。在 Kotlin 中,类属性实际上是用单独的 getter 和 setter 方法实现的。当查看类的代码试图确定底层 JavaBeans 属性的名称时,它会从方法名称中去掉“get”、“has”和“is”,然后将下一个的大小写小写信件。这就是 Firestore SDK 正在做的事情。

    如果您不希望它使用默认行为来将访问器方法的名称映射到文档属性的名称,您可以使用键/值对自己编写Map<String, Any>,或apply the @PropertyName annotation on the class properties

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      相关资源
      最近更新 更多