I'm getting an error while using a list of custom object with Room.
error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
So my data class is like this:
@Entity(tableName = "calorie_daily_table")
data class CalorieDaily(
@PrimaryKey
var date: String,
var breakfast: List<Meal>)
data class Meal(
var foodItem: String,
var foodQuantity: String,
var calorie: Int)
How can i write a TypeConverter to handle this?
So, I've something like this:
class Converter {
var gson = Gson()
@TypeConverter
fun foodItemToString(foodItems: List<FoodItem>): String {
return gson.toJson(foodItems)
}
@TypeConverter
fun stringToFoodItem(data: String): List<FoodItem> {
val listType = object : TypeToken<List<FoodItem>>() {
}.type
return gson.fromJson(data, listType)
}
}
But, still getting the same error.