Member-only story

Exploring Different Ways to Collect Kotlin Flow

Simple app to demonstrate Kotlin flow(), emit(), collectAsState(), collect(), viewModelScope.launch(), launchWhenStarted() and repeatOnLifecycle()

Vincent Tsen
5 min readJan 13, 2023

This is part of the asynchronous flow series:

Basic Kotlin Flow Usages

You first need to create a flow before you can collect it.

Create Flow<T> and Emit T Value

1. Create Flow<T> Using kotlinx.coroutines.flow.flow()

The first parameter of the flow() is the function literal with receiver. The receiver is the implementation of FlowCollector<T> interface, which you can call FlowCollector<T>.emit() to emit the T value.

--

--

No responses yet