0

I'm trying to create a Wrapper class for handling the api call in retrofit

Wrapper sealed class

sealed class NetworkResult<T : Any> {
    class Success<T: Any>(val data: T) : NetworkResult<T>()
    class Error<T: Any>(val code: Int, val message: String?) : NetworkResult<T>()
    class Exception<T: Any>(val e: Throwable) : NetworkResult<T>()
}

In the fetchCustomUI() function, I'm making the api call

Here I'm getting the compile time error Type mismatch. Required:T Found:CustomUiResponse

suspend fun <T:Any> fetchCustomUI(url:String):NetworkResult<T> {
       
 return try{ 
             val response = api.getCustomUI(url)
             val body = response.body()
             if (response.isSuccessful && body != null)
                   NetworkResult.Success(body)       // This is where I'm receiving the error
             else
                   NetworkResult.Error(response.code(), response.message())
          
          } catch(Exception e){
                   NetworkResult.Exception(e)
          }
   }

My retrofit service interface

interface ApiInterface {
    @GET
    suspend fun getCustomUI(@Url url:String): Response<CustomUiResponse>
}
newbie_coder
  • 225
  • 2
  • 9
  • 1
    You do not need generic type in method signature .. Just use `CustomUiResponse` in place of T – ADM Sep 25 '22 at 06:56

0 Answers0