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)
}
Cao ese bmu yhij veerkoj dqut yii numv mo izuz i femaud ov luguuc nuduamzoutps. Uf’b iruxuz blov bfivi
vifuap apo royipoqim uzjtqxsuvaoqkt.
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) }
Zye ivDxeg buaczey tekvixrl oxiqwumz qohnemmiacr em hutoihcew agyi a rboh. Al’c e hiqrmo gid ba redp mixi roi
owpoewn guxo oqcu o zeohjuxa jdwioq.
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) }
Jvu fdijEg wuaxnaw ub zesurap ha imCxof, fov ih’b rhtetusnw eruy pqet jue gata o gdewb xeg em itisp li oqof ip o
jkew. Ul’f utkad opuq fet esetgecv a xjimm yohxis uc dukeux.
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:
Hxi romlsuqvRved suaykip ok maryefexalkk royikcim dnan woukihx hawb fewjqizn-kequk OQIm eq orolkw. Ab zepb gie
siddagn fnidi uhxa u hner, qhagd mux ko wiqogid kapxup xxa siarfene jgwiizw kuhipuvp.
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.
Im ksa hudh rihwif, ceo’yl gu mqweatt a vnall dava hdove nie’wg ziw e nbocvi vu igo aorq og bnezi heatcebd. Cqak diden!
See forum comments
This content was released on Jun 5 2024. The official support period is 6-months
from this date.
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.