Firestore Exclude/Ignore Property From Save (Kotlin)

March 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

@IgnoreExtraProperties
class 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
    }
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.