类型推断失败。预期类型不匹配

塞巴斯蒂安·费尔南德斯(Sebastian Fernandez)

下面的代码显示该错误。类型推断失败。所需的预期类型不匹配Response<BaseResponse<Any>>! 找到Response<BaseResponse<RetriveUserInfoResponse>!>!

`when`( mockIOnboardingService.validateCustomerIdentity(customerType.toLowerCase(), ValidateCustomerRequest(customerId, documentType, "243546", tyc)))
.thenReturn(Response.success(BaseResponse(payload = RetriveUserInfoResponse("+5689765432")))) //--> Here the error

这是validateCustomerIdentity方法

@POST(ApiConstants.bffOnboardingPath + ApiConstants.pathRetriveUserInfo)
    suspend fun validateCustomerIdentity(
        @Header(ApiConstants.headerXflowService) customerType : String,
        @Body body: ValidateCustomerRequest
    ): Response<BaseResponse<Any>>

如您所见,它返回BaseResponse<Any>为什么Android Studio向我显示BaseResponse<RetriveUserInfoResponse>!错误

这是RetrieveUserInfoResponse数据类

data class RetriveUserInfoResponse(
    @SerializedName("phone")
    val phone: String
)
亚当·米勒奇普

这个问题是Response.success(BaseResponse(payload = RetriveUserInfoResponse("+5689765432")))产生一个Response<BaseResponse<RetriveUserInfoResponse>>与的类型(或子类型)不同的Response<BaseResponse<Any>>

您可以通过铸造修复RetriveUserInfoResponseAny

Response.success(BaseResponse(payload = RetriveUserInfoResponse("+5689765432") as Any))

或者通过将返回类型更改validateCustomerIdentity()Response<out BaseResponse<out Any>>,这是有效的,因为它Response<BaseResponse<RetriveUserInfoResponse>>是的子类Response<out BaseResponse<out Any>>

@POST(ApiConstants.bffOnboardingPath + ApiConstants.pathRetriveUserInfo)
suspend fun validateCustomerIdentity(
    @Header(ApiConstants.headerXflowService) customerType : String,
    @Body body: ValidateCustomerRequest
): Response<out BaseResponse<out Any>>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型不匹配:推断的类型为()-> JoinColumn,但应为JoinColumn

Android检查文件错误“类型不匹配:推断的类型为上下文,但预期路径!”

参数值与预期类型不匹配

类型不匹配:推断的类型为T但kotlin。

错误:类型推断失败

类型不匹配的推断类型为单位,但预期为无效

Kotlin推断类型不匹配

类型不匹配:推断的类型为Int,但应为Byte

类型不匹配:推断的类型是但预期

错误Kotlin类型推断失败预期类型不匹配Map <MessageDestination,List <MessageSender >>

类型不匹配:推断的类型为LoginActivity,但预期为LifecycleOwner

类型不匹配:推断的类型为Fragment,但应使用YouTubePlayerSupportFragment

Kotlin:类型推断失败。预期的类型不匹配:推断的类型为MutableList <Long?>,但预期的是MutableCollection <Long>

类型不匹配:推断的类型为NameFragment但预期使用上下文-Kotlin

预期类型与收到的类型不匹配

打包功能中的预期类型不匹配

Scala-推断为错误的类型,导致类型不匹配?

休眠:参数值与预期类型不匹配?

错误:参数值与预期类型不匹配

Kotlin 类型推断失败

Array() 与预期类型“对象”不匹配

kotlin - 类型不匹配:推断类型是 MutableList<TypeA> 但 MutableList<InterfaceType> 是预期的

Kotlin:“类型推断失败”

类型不匹配:推断的类型是 Result<List<DataItem>?> 但 List<DataItem> 是预期的

JPA + Hibernate currentTimestamp() 与预期类型不匹配

错误:类型不匹配:推断的类型是字符串?但布尔值是预期的

Kotlin - 类型不匹配:推断的类型是 Any?但布尔值是预期的

我在 PagingSource 类中收到错误“类型不匹配:推断类型是 List<Transactions.Past>?但 List<TypeVariable(Value)> 是预期的”

我在 PagingSource 类中收到错误“类型不匹配:推断类型是 List<Transactions.Past>?但 List<TypeVariable(Value)> 是预期的”