I'm trying to use `MutableStateFlow` for the first...
# getting-started
d
I'm trying to use
MutableStateFlow
for the first time, but it is not doing what I expect
Copy code
private val state = MutableStateFlow<MyState>(INITIAL_STATE)

suspend fun refresh(): MyState {
  val newState = service.refresState()
  this.state.value = newState
  return newState
}

fun observeState(): Flow<MyState> {
  return state
    .transform { s ->
        if (s !== INITIAL_STATE) {
            emit(s)
        } else {
            refresh() // expecting this to automatically send a new state to the flow
        }
    }
}
I was expecing the
refresh()
to cause a new state to be emitted, but it didn't. Can someone guide me in the right direction? thanks.
j
is it an actual new object? StateFlow will only emit if it has a new (non-equal) object
If my memory serves me right
d
oh, I didn't know it had a comparison in it, let me try changing it a bit
ah! it was clearly stated in the doc, I missed it, thanks
📃 1