collect

abstract suspend override fun collect(collector: FlowCollector<T>): Nothing(source)

Accepts the given collector and emits values into it. To emit values from a shared flow into a specific collector, either collector.emitAll(flow) or collect { ... } SAM-conversion can be used.

A shared flow never completes. A call to Flow.collect or any other terminal operator on a shared flow never completes normally.

It is guaranteed that, by the time the first suspension happens, collect has already subscribed to the SharedFlow and is eligible for receiving emissions. In particular, the following code will always print 1:

val flow = MutableSharedFlow<Int>()
launch(start = CoroutineStart.UNDISPATCHED) {
flow.collect { println(1) }
}
flow.emit(1)

See also

for implementation and inheritance details.