Home > Net >  Not enough information to infer variable T: How to structurate service method into Retrofit2 Kotlin
Not enough information to infer variable T: How to structurate service method into Retrofit2 Kotlin

Time:09-22

I was developing an App in kotlin, which connect with an example API which return some users information, in order to test the library of Retrofit.

I developed the main structure of the App, an I get which the follofing error when I try to implement the funtionality of select by Id, into the API.

Not enough information about variable T

The code where comes this error is in the Provider an the service as I define my proyect.

The code of the Model data is the following:

data class QuoteModel(
    @SerializedName("id") val quote: Number,
    @SerializedName("name") val name: String,
    @SerializedName("email") val email: String,
    @SerializedName("address") val addrees: String,
    @SerializedName("phone") val phone: String,
    @SerializedName("website") val website: String,
    @SerializedName("company") val company: String,
    )

My provider is the next:

@Singleton
class QuoteProvider @Inject constructor() {
    var allUsers: List<QuoteModel> = emptyList1()
    var userById: QuoteModel = emptyList1() as QuoteModel

}

my Service is this:

class QuoteService @Inject constructor(private val api:QuoteApiClient) {

    suspend fun getAllUsers(): List<QuoteModel> {
        return withContext(Dispatchers.IO) {
            val response = api.getAllUUsers()
            response.body() ?: emptyList1()
        }
    }

    suspend fun getUserById(id:Number): QuoteModel{
        return withContext(Dispatchers.IO){
            val response = api.getUserById(id)
            response.body() ?: emptyList1() as QuoteModel --here is the error
        }
    }

}

The error comes in this line: response.body() ?: emptyList1() as QuoteModel

I know that is a type variable error but I start just a few mounth more serious with Kotlin, and I don't know if exit something similar to emptyList(), but with just one object.

Take thanks in advance !

CodePudding user response:

You can use listOf<QuoteModel>() for creating empty array you are trying to return list in second function but it requires QuoteModel . So you should create an empty QuoteModel object or return null using QuoteModel? in return type

  • Related