James Black
04/17/2022, 2:10 AMfun addClothingToClothingWeather(clothing:ClothingTypes) {
if(clothingWeatherData == null) clothingWeatherData = ClothingWeatherModel()
clothingWeatherData!!.clothing?.toMutableList()?.add(clothing)
}
@Serializable
data class ClothingWeatherModel(val clothing:List<ClothingTypes>? = emptyList(), val temperature:MainTemperature? = null, val situation:Situations? = null)
Rick Clephas
04/17/2022, 6:10 AMclothingWeatherData
variable and possibly set it to null
. So you can’t really guarantee that it isn’t null
.
FYI toMutableList
actually makes a copy of your list, so the added clothing
won’t be part of the original list in the ClothingWeatherModel
.
Something like the following would work:
@Serializable
data class ClothingWeatherModel(
val clothing:List<ClothingTypes>? = emptyList(),
val temperature:MainTemperature? = null,
val situation:Situations? = null
)
var clothingWeatherData: ClothingWeatherModel? = null
fun addClothingToClothingWeather(clothing:ClothingTypes) {
val data = clothingWeatherData ?: ClothingWeatherModel()
val list = data.clothing?.toMutableList()?.apply { add(clothing) } ?: listOf(clothing)
clothingWeatherData = data.copy(clothing = list)
}
James Black
04/17/2022, 5:50 PM