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:
@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