am trying to handle saving state across process de...
# android
m
am trying to handle saving state across process death using
SavedStateHandle
and I want to introduce
Flow
like this:
Copy code
class MyViewModel<State>(
    private val savedStateHandle: SavedStateHandle,
    initialState: State,
) : ViewModel() {
    private var _state by mutableStateOf(initialState)
    val state: State
        get() = _state

    init {
        viewModelScope.launch {
            savedStateHandle.getFlow(STATE, initialState)
                .collect { _state = it }
        }
    }

    fun updateState(reducer: State.() -> State) =
        savedStateHandle.set(STATE, state.reducer())

    companion object {
        private const val STATE = "state"
    }
}
it crashes my tests but when I do regular
get
,
set
on
savedStateHandle
tests work fine. both work great when I run in on emulator or real device but every single test crashes. any help?
stackoverflow 1
👍 1
savedStateHandle.getFlow
is just extension function ->
savedStateHandle.getLiveData().asFlow
v
Might be that your test class needs to be annotated with @RunWith(RoboelectricTestRunner::class)
m
why? I have test dispatchers all set up and it worked great until I refactored it