Firestore Exclude/Ignore Property From Save (Kotlin)

Mar 1, 2019
Why @Exclude and @IgnoreExtraProperties doesn't work

To exclude a field/property from Firebase, you need to use @get:Exclude.

class Test(    var name: String = "",    @get:Exclude val isLocalFlag: Boolean = true) {    @get:Exclude    val notLocalFlag: Boolean        get() = !isLocalFlag}

@IgnoreExtraProperties and @Exclude doesn't seems to work, probably because getter is generated by Kotlin.

NOTE: Firestore Annotations

@IgnoreExtraPropertiesclass Test(    @Exclude val excludeNotWorking: Boolean = true, // fail    @get:Exclude val isLocalFlag: Boolean = true) {    val ignoreExtraPropertiesNotWorking: Boolean // fail        get() = !isLocalFlag    @get:Exclude    val notLocalFlag: Boolean        get() = !isLocalFlag}

If you have kotlin function which return value, @Exclude annotation is required as well to prevent those value to be saved.

class Test(    val publishStatus: Int = 0,    val isActive: Boolean = true) {    @Exclude    fun getStatusText(): String {        return if (isActive) "ACTIVE" else "INACTIVE"    }    @Exclude    fun isImutable(): Boolean {        return publishStatus == 1    }}

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.