Search code examples
androidkotlingson

Gson error "Not enough information to infer type variable T" when passing the TypeToken


I want to deserialize the list of maps that I return from a different activity. Here is my code:

val listType = object : TypeToken<List<Map<String, Any>>>() {}.type
val stepsList = Gson().fromJson(resultStr,  listType)

fromJson() here shows the error "Not enough information to infer type variable T."

I don't understand how this is possible when I literally specify the TypeToken for my object. I tried a couple different methods of getting the list type (including the inline functions) but they all result in the same error. How can I make it work?


Solution

  • You have to specify the type of the fromJson call:

    Gson().fromJson<List<Map<String, Any>>>(resultStr, listType)
    

    This should fix the error.