Home > OS >  Issue in Type Converter of Room Database Kotlin
Issue in Type Converter of Room Database Kotlin

Time:08-31

Type Convertor Class :

class ProductTypeConvertor {

var gson = Gson()

@TypeConverter
fun foodRecipeToString(foodRecipe: ProductList): String {
    return gson.toJson(foodRecipe)
}

@TypeConverter
fun stringToFoodRecipe(data: String): ProductList {
    val listType = object : TypeToken<ProductList>() {}.type
    return gson.fromJson(data, listType)
}

@TypeConverter
fun resultToString(result: Products): String {
    return gson.toJson(result)
}

@TypeConverter
fun stringToResult(data: String): Products {
    val listType = object : TypeToken<Products>() {}.type
    return gson.fromJson(data, listType)
}

@TypeConverter
fun stringToVListServer(data: String?): List<Variants?>? {
    if (data == null) {
        return Collections.emptyList()
    }
    val listType: Type = object :
        TypeToken<List<Variants?>?>() {}.type
    return gson.fromJson<List<Variants?>>(data, listType)
}

@TypeConverter
fun VlistServerToString(someObjects: List<Variants?>?): String? {
    return gson.toJson(someObjects)
}

@TypeConverter
fun stringToListServer(data: String?): List<String?>? {
    if (data == null) {
        return Collections.emptyList()
    }
    val listType: Type = object :
        TypeToken<List<String?>?>() {}.type
    return gson.fromJson<List<String?>>(data, listType)
}

@TypeConverter
fun listServerToString(someObjects: List<String?>?): String? {
    return gson.toJson(someObjects)
 }     
}

Product Entity :

  @ColumnInfo(name = "other_images")
  var other_images: ArrayList<String>  = arrayListOf(),

  @ColumnInfo(name = "variants")
  var variants : ArrayList<Variants> = arrayListOf()

Error : error: incompatible types: List cannot be converted to ArrayList _tmpVariants = __productTypeConvertor.stringToVListServer(_tmp_3);

error: incompatible types: List cannot be converted to ArrayList _tmpOther_images = __productTypeConvertor.stringToListServer(_tmp);

CodePudding user response:

The types do no match it's like the anecdotal apples(List type) and oranges(ArrayList type).

Either use ArrayList or List as both

  • a) the column type and
  • b) as the input parameter and the result from the converters.
  • Related