没有足够的信息来推断变量 T:如何将服务方法结构化为 Retrofit2 Kotlin App
Not enough information to infer variable T: How to structurate service method into Retrofit2 Kotlin App
我正在用 kotlin 开发一个应用程序,它连接了一个示例 API 其中 return 一些用户信息,以测试 Retrofit 的库。
我开发了应用程序的主要结构,当我尝试通过 Id 将 select 的功能实现到 API.
时,出现以下错误
Not enough information about variable T
出现此错误的代码在 Provider 和我定义我的项目的服务中。
模型数据代码如下:
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,
)
我的提供商是下一个:
@Singleton
class QuoteProvider @Inject constructor() {
var allUsers: List<QuoteModel> = emptyList1()
var userById: QuoteModel = emptyList1() as QuoteModel
}
我的服务是这样的:
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
}
}
}
错误出现在这一行:response.body() ?: emptyList1() as QuoteModel
我知道这是一个类型变量错误,但我开始使用 Kotlin 时更严重,我不知道是否退出类似于 emptyList() 的东西,但只有一个对象。
提前致谢!
您可以使用 listOf<QuoteModel>()
创建空数组
您正在尝试 return 在第二个函数中列出,但它需要 QuoteModel 。所以你应该创建一个空的 QuoteModel 对象或 return null using QuoteModel?
in return type
我正在用 kotlin 开发一个应用程序,它连接了一个示例 API 其中 return 一些用户信息,以测试 Retrofit 的库。
我开发了应用程序的主要结构,当我尝试通过 Id 将 select 的功能实现到 API.
时,出现以下错误Not enough information about variable T
出现此错误的代码在 Provider 和我定义我的项目的服务中。
模型数据代码如下:
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,
)
我的提供商是下一个:
@Singleton
class QuoteProvider @Inject constructor() {
var allUsers: List<QuoteModel> = emptyList1()
var userById: QuoteModel = emptyList1() as QuoteModel
}
我的服务是这样的:
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
}
}
}
错误出现在这一行:response.body() ?: emptyList1() as QuoteModel
我知道这是一个类型变量错误,但我开始使用 Kotlin 时更严重,我不知道是否退出类似于 emptyList() 的东西,但只有一个对象。
提前致谢!
您可以使用 listOf<QuoteModel>()
创建空数组
您正在尝试 return 在第二个函数中列出,但它需要 QuoteModel 。所以你应该创建一个空的 QuoteModel 对象或 return null using QuoteModel?
in return type