Sobes.tech
Junior — Middle

Is it possible to set a starting value for SharedFlow when creating it?

sobes.tech AI

Answer from AI

In Kotlin Flow, SharedFlow does not support setting an initial value upon creation, unlike StateFlow, which always requires a starting value.

SharedFlow is a hot stream that does not hold state by default and does not have an initial value. If you need a stream with the current state and an initial value, you should use StateFlow.

Example of creating a StateFlow with an initial value:

val stateFlow = MutableStateFlow(0) // initial value 0

For SharedFlow, you can configure the buffer and subscription behavior, but there is no initial value.