I'm trying to set up a ViewModel but I'm having trouble updating UiState fields. The UiState.copy()
function should return a modified UiState
object but just returns unit.
I expect the value to change once the code runs. I cant get it to compile tough because the lambda body has the wrong return type.
This is my code:
class CalendarViewModel: ViewModel() {
private val _calendarUiState = MutableStateFlow(CalendarUiState())
val calendarUiState: StateFlow<CalendarUiState> = _calendarUiState.asStateFlow()
fun test() {
_calendarUiState.update(
{ calendarUiState -> calendarUiState.copy(currentDate = "new_date") }
)
}
}
I get this while hovering over the lambda:
Type mismatch.
Required:
CalendarUiState
Found:
Unit
The problem was in the UiState-Class, I didnt declare it as a data class. Check your UiState file if you have a similar problem