Home > Back-end >  by mutableStateOf seem to fail in this situation
by mutableStateOf seem to fail in this situation

Time:01-13

I have a funny one

class RoleState(
    var roles: List<String> = emptyList(),
    var selectedRoles: List<String> = emptyList()
)

in my ViewModel I have

var rolesState by mutableStateOf(RoleState())

enter image description here Below

rolesState = rolesState.copy(
    selectedRoles = result.data ?: kotlin.run {
        _eventFlow.emit(value =UiEvent.ShowSnackbar(uiText = UiText.errorUnknown()))
        return@launch
    }
)

Normally .copy would work, but now emits Unresolved reference: copy

Only reason I can think of is that the State class needs to be primatives and Data Classes only?

CodePudding user response:

RoleState has to be a data class in order to get the compiler to create a copy function. If you don't want to make it a data class you have to provide/implement the copy function on yourself.

See the documentation on data classes for more information.

  • Related