Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Type mismatch: inferred type is data class but Int was expected

I’m fetching data from API. But I’m facing a problem, the LazyColumn items is not accepting the state vairable. I have this error:

Type mismatch: inferred type is Weather? but Int was expected

code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  @GET("/v1/forecast.json")
 suspend fun getWeatherInfo(
    @Query("key") apiKey:String = API_KEY,
    @Query("q") country:String
):Weather


 suspend fun getWeatherInfo(country:String):Weather{
    return weatherApi.getWeatherInfo(country = country)
}



private val _state = mutableStateOf(WeatherState())
val state: State<WeatherState>
    get() = _state

data class Weather(
    @SerialName("current")
    val current: Current,
    @SerialName("forecast")
    val forecast: Forecast,
    @SerialName("location")
    val location: Location
)

LazyColumn{

    items(state.weather){

    }

Any help?

>Solution :

So you are using wrong method. You should consider not using LazyColumn, as you probably do not need it, but if you do, it would be something like this:

val mutableState = mutableStateOf(WeatherState())

@Composable
fun LazyListWeather() {
    LazyColumn {
        item {
            Text(text = mutableState.value.toString())
        }
    }
}

But IMO you do not need. You have only one Weather item. Just use simple column, if you do not have list of WeatherState

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading