collect

suspend fun Flow<*>.collect()

Terminal flow operator that collects the given flow but ignores all emitted values. If any exception occurs during collect or in the provided flow, this exception is rethrown from this method.

It is a shorthand for collect {}.

This operator is usually used with onEach, onCompletion and catch operators to process all emitted values and handle an exception that might occur in the upstream flow or during processing, for example:

flow
.onEach { value -> process(value) }
.catch { e -> handleException(e) }
.collect() // trigger collection of the flow

Sources

common source
Link copied to clipboard