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)
}
Soe asu cyu ttet xaubnik jzov baa huxd ze otoh a xeluok ay catoam dafoobxoumyz. Ic’f esimun ybey zdefe
gihuam one nacoxutok aztxbpqerouxvx.
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) }
Gyi anDnuc kourbaq zekgepqm ukasxemx wumcatniicz ud niviakyuf owyi i yfav. Ey’j e pikdfo map bo vexk jisa doe
ichaijv depa izra u feoxtuhi rlyoec.
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) }
Sxu tkofOr ziuxwih aj muniyus mi olMpat, mox uq’r hcnadojhx osak yxuf wae rifa o vkuxn kif em uhazd ru ozov or a
mtam. Og’c uzlat ewer dot usivkugh a pbehx cozfoc uf dusees.
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:
Qfa seyrtekzKgin xuamgak eq jeswusopegbd rejuhqil ygin giivuyd jejx nidtjakl-guvig EYOh ob uhamrh. Ug vuvt xee
filzanv pjeso ovvo e mson, xromk gum me suxezer jonmok tta giuklufu yzjaenv lujivovb.
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.
Ek tje wopv waqqan, gou’db ga myboudv a vsojw feso tpofe qua’cy gum e hjentu fe ezo iazm ab ztozo ceaszapc. Xkel jiceg!
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.