class RetrofitInstance {
companion object {
private val retrofit by lazy {
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
val client = OkHttpClient.Builder()
.addInterceptor(logging)
.build()
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
}
val api by lazy {
retrofit.create(RickAndMortyServiceApi::class.java)
}
} }
Error message
"Type 'Lazy<TypeVariable(T)>' has no method 'getValue(RetrofitInstance.Companion, KProperty<*>)' and thus it cannot serve as a delegate"
Kotlin version 1.6
CodePudding user response:
I have tried this code snippet on my machine, and it works fine:
companion object {
private val retrofit by lazy {
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
val client = OkHttpClient.Builder()
.addInterceptor(logging)
.build()
Retrofit.Builder()
.baseUrl("https://www.google.com")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
}
val api by lazy {
retrofit.create(DummyClass::class.java)
}
}
So i suggest you double-check your code for typos, or you can check for dependency versions. Versions that i'm using are:
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation("com.squareup.okhttp3:logging-interceptor:4.9.3")
CodePudding user response:
Please try to import the following and also try the "=" instead of "by" may help you
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.*