Member-only story
Exploring Different Ways to Collect Kotlin Flow
Simple app to demonstrate Kotlin flow(), emit(), collectAsState(), collect(), viewModelScope.launch(), launchWhenStarted() and repeatOnLifecycle()
5 min readJan 13, 2023
This is part of the asynchronous flow series:
- Part 1 — Exploring Android LiveData Usages and Behaviors
- Part 2 — Introduction to Kotlin Flows and Channels
- Part 3 — Exploring Different Ways to Collect Kotlin Flow
- Part 4 — Convert Flow to SharedFlow and StateFlow
- Part 5 — Flow, SharedFlow, StateFlow Class Diagram
- Part 6 — Kotlin Flow — Combine, Merge and Zip
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.