29

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)

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.

Are there any workarounds?

koceeng
  • 2,169
  • 3
  • 16
  • 37
Dan
  • 355
  • 3
  • 7
  • From what I read it should be the same but does prefixing `Exclude` with "field:" make any difference? `data class User(val name: String = "", @field:Exclude val age: Int = 0)` – mfulton26 Oct 18 '16 at 19:37
  • 1
    Nope, doesn't make any difference, but @get:Exclude works (found it thanks to you). If you want post it as an answer and I'll accept it. – Dan Oct 18 '16 at 19:57
  • 2
    Ah, and that makes sense now that you mention it too. Team effort! – mfulton26 Oct 18 '16 at 20:10

2 Answers2

67

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.

mfulton26
  • 29,956
  • 6
  • 64
  • 88
11

Actually you don't need to add only @get:Exclude but you need all 3 Exclude,

@Exclude @set:Exclude @get:Exclude.

I did it for imageUrl and providerId

data class FirebaseChatModel(
        @get:PropertyName("message")
        @set:PropertyName("message")
        var message: String = "",
        @get:PropertyName("type")
        @set:PropertyName("type")
        var type: Int = 1,
        @get:PropertyName("senderId")
        @set:PropertyName("senderId")
        var senderId: Int = 0,
        @get:PropertyName("receiverId")
        @set:PropertyName("receiverId")
        var receiverId: Int = 0,
        @Exclude @set:Exclude @get:Exclude var imageUrl: String? = "",
        @Exclude @set:Exclude @get:Exclude var providerId: Int = 0
)
Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82