7

I was trying to make a dataclass for Room and the class also have a field which is for the views only. I do not want to save the data into Room.

@Entity(tableName = "MyTable")
@Parcelize
data class MyTable(
    @SerializedName("id") @PrimaryKey val id: String,
    @SerializedName("field1") val field1: String?,
    var selected: Boolean? = false //todo use @Ignore
) : Parcelable

The above code works. However whenever I tried to use @Ignore annotation with the variable with selected variable. Its giving me the following error

error: Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).

If I remove the variable from the constructor like

    @Entity(tableName = "MyTable")
    @Parcelize
    data class MyTable(
        @SerializedName("id") @PrimaryKey val id: String,
        @SerializedName("field1") val field1: String?

    ) : Parcelable{
    var selected: Boolean? = false //todo use @Ignore
    }

The field selected will not be written into the parcel. How can I keep the variable into the class without making a column and still keeping it into the parcel?

Thanks

sadat
  • 4,004
  • 2
  • 29
  • 49
  • I think you can just have an empty constructor. Do you get any error with that? – Reaz Murshed Jul 08 '19 at 01:45
  • I tried with empty constructor. still its not working. I also used @ignore with the constructor. but didnt work – sadat Jul 08 '19 at 03:05
  • @Parcelize requires all serialized properties to be declared in the primary constructor. So I think you have to implement your on custom parcelisation. I hope this answer ca give you more clarity https://stackoverflow.com/questions/53312247/how-to-parcelise-member-variable-other-than-constructor-in-data-class-while-usin – Sonu Sanjeev Jul 08 '19 at 06:25
  • did you trying using without data class and just normal class? – karandeep singh Jul 08 '19 at 11:11

1 Answers1

4

@Ignore in such a case currently requires @JvmOverloads annotation:

data class MyTable @JvmOverloads constructor(
    ...
    @Ignore var selected: Boolean? = false
)
gmk57
  • 839
  • 11
  • 20