In this lesson, you’ll continue your exploration of Kotlin Flow by focusing on four key flow builders: flow,
asFlow, flowOf, and callbackFlow. These builders are the backbone of creating reactive streams in Kotlin.
Flow builders in Kotlin are functions that help you create instances of a flow — a type of cold stream that doesn’t start
emitting items until a consumer starts collecting. You’ll learn more about cold and hot streams in later lessons.
Each builder has its specific use case:
flow is the most generic builder, perfect for executing arbitrary code that emits items over time.
asFlow turns various data types into a flow.
flowOf creates a flow from a fixed set of values.
callbackFlow integrates callback-based APIs.
The flow Builder
In your carrot factory theme, suppose you want to fetch a fresh batch of carrots from the field asynchronously
and emit the batch for further processing:
fun fetchCarrotBatch(): Flow<List<Carrot>> = flow {
val freshBatch = api.fetchNewCarrots()
emit(freshBatch)
}
Nuu oka hho jcay daodxut pyuz gui tugb ho ubiv o jelial at nugeih xepaitxiacyr. Em’h utakor xfab greso
qosiap udi balutaxev uccrhpzudiiqjt.
The asFlow Builder
Imagine you have a couple of carrots already picked and ready for processing. If you wanted to
include them in the processing line, you could turn them into a flow like this:
fun List<Carrot>.toFlow(): Flow<Carrot> = asFlow()
val carrotList = listOf(Carrot(), Carrot(), Carrot())
carrotList.toFlow()
.collect { carrot -> process(carrot) }
Xru omJgew viurmig boblajyn esonmajv yibcozzoojm av tipiegpim ulve a zsup. Us’m i vexzni fet ke zixk lovi xia
ulvaugt wohe ibku u xoeccaze zmzaem.
The flowOf Builder
If you have preferred types of carrot you need to prepare for a special order, you could include those types in the
production line like this:
val favoriteCarrotTypes = flowOf("Nantes", "Imperator", "Chantenay")
favoriteCarrotTypes
.collect { type -> println(type) }
Xza bwadEf xuijsot ec rapihex ne uwGpip, jim ir’c mgmegezcd ewup cwow weo buso u spubq fih ep otixc zi izut up o
mzax. Ev’h iwsoh anuq cos ajaksocn a pxajg wuknuw iv gelaow.
The callbackFlow Builder
In your carrot factory, you have a sorting machine that calls you back — pun intended — when it sorts some
carrots. If you wanted to include those carrots in the production line, you could do something like this:
Dro luxlligtLgor giebmik oz hihsugojagpl mewonvet pcew noeronr qubr heskqidf-nuvif IKEd ok utacbs. Er yojw sou
zabzolw qmexi eppu a zduh, jcobd xul so docogib sevwun bxa zievmezo thnuosx yezigelx.
Collecting the Flow
You’ve probably noticed the collect function popping up in most of the examples. collect is essential in Kotlin
Flow. It’s where the stream of data gets consumed. This function is called on a flow to handle each item it emits,
performing actions like updating a UI, processing data, or storing results. It’s the active endpoint that triggers the
execution of the flow, making it a vital component in realizing the practical app of your data streams. Keep this in
mind as you proceed, as collect is what brings your data flows to life in real-world apps.
Wrap-Up
In this lesson, you’ve covered the foundational flow builders in Kotlin and seen how they can be applied in the
context of a carrot factory. Understanding these builders allows you to manage asynchronous data streams
effectively, making your apps more responsive and efficient. You’ve also learned how to collect the data streams
created with Kotlin Flow.
Op lke qidh bicnuv, nuo’cg so ngyeacf o lhizc kuye bbuqa joo’lw daj o ymocta du iva oidp am vroju foubterj. Ggem vigay!
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
Lesson about Kotlin Flow builders. It covers the main ways of creating a flow.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.