Firebase @Exclude与Kotlin数据类 [英] Firebase @Exclude with kotlin data class

查看:96
本文介绍了Firebase @Exclude与Kotlin数据类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Kotlin中有此数据类(示例):

I have this data class in Kotlin (example):

import com.google.firebase.database.Exclude

data class User(val name: String = "", @Exclude val age: Int = 0)

而且我不想将age属性保存在firebase中. @Exclude应该执行此操作,但是它不起作用,仍然可以保存年龄.

And I don't want to save the age property in firebase. @Exclude should do this but it does not work, age is still saved.

有没有解决方法?

推荐答案

在属性上放置@Exclude会针对其生成的字段,而不是其生成的get accesor方法.要执行后者,您需要在"Exclude"之前加上"get:"前缀.例如:

Placing @Exclude on a property targets its generated field and not its generated get accesor method. To do the latter you'll need to prefix "Exclude" with "get:". e.g.:

data class User(val name: String = "", @get:Exclude val age: Int = 0)

有关更多详细信息,请参见注释使用站点目标.

See Annotation Use-site Targets for more details.

这篇关于Firebase @Exclude与Kotlin数据类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆