Hi everyone, is there a way to get the the previou...
# flow
t
Hi everyone, is there a way to get the the previously emmited value of a flow in the onEach function, or something like this?
Copy code
private val refreshJob = filterFlow
    .onEach { prev, new ->
        if(prev.x != new.x) {
            updateX()
        }
    }
    .launchIn(viewModelScope)
I know I can separate the filterFlow in multiple flows, which is probably the better way of solving my use case, but I'm still curious
e
Copy code
filterFlow.fold(null) { prev: T?, new ->
    if (prev == null || prev.x != new.x) {
        updateX()
    }
    new
}
👍 1
or
Copy code
filterFlow.reduce { prev, new ->
    if (prev.x != new.x) {
        updateX()
    }
    new
}
if you don't need that initial unpaired value
👍 2