What is the difference between using value end emit fun on a MutableStateFlow?
fun main() = runBlocking {
val mutable = MutableStateFlow(0)
launch {
mutable.collect {
println(it)
}
}
mutable.value = 1
mutable.emit(2)
}
emit()
is a suspend function that wraps a call to set the value:
override suspend fun emit(value: T) {
this.value = value
}
So the difference is that value
lets you set the value even when not in a coroutine. emit()
exists so MutableStateFlow can inherit from MutableSharedFlow.
Source code here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With