共有コードのFlowの基本
commonMainから値のストリームを発行します
「共有コードのFlowの基本」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Streams of Values
A suspend function returns one value, then it is done. A Flow is different: it emits a whole stream of values over time, one after another. 🌊
Cold by Default
A Flow is cold: the code inside it does nothing until someone starts collecting. No collector means no work, which keeps your shared code lazy and cheap.
Build One with flow {}
The flow builder is the simplest way to create a stream. Inside it you call emit to push each value out to whoever is listening.
fun ticks(): Flow<Int> = flow {
emit(1)
emit(2)
emit(3)
}emit Pushes a Value
Each emit call hands one value to the collector before moving on. The stream is finished only when the builder block returns.
Collect to Receive
To read a Flow you collect it. Collecting is a suspend call, so it lives inside a coroutine and runs your block for every emitted value.
ticks().collect { value ->
println(value)
}It Works Everywhere
Flow ships in kotlinx-coroutines, which is multiplatform. The exact same flow code in commonMain runs on Android and iOS with zero changes. 🎉
flowOf for Fixed Values
When you already know the values, flowOf wraps them into a Flow instantly. It is the quickest stream you can make.
val names = flowOf("Ana", "Beto", "Cem")Turn a List into a Flow
Any Iterable can become a stream with asFlow. This is handy when you already have a list and want to process it one item at a time.
val stream = listOf(10, 20, 30).asFlow()Emit Over Time
Because the builder is a suspend block, you can delay between emissions. That models real streams like ticks, sensor readings, or polling.
flow {
repeat(3) { i ->
delay(1000)
emit(i)
}
}Flow vs suspend
Reach for a suspend function for a single async result, and a Flow when values keep arriving. Choosing the right one keeps your API honest.
Why It Fits Shared Code
Flow lets your shared layer push data as it changes, and each app just listens. Your business logic drives the UI without knowing which platform it is on.
Quick Check
Let us make sure the basics stuck.
Recap
You met Flow: a cold stream you build with flow and read with collect. It is multiplatform, so the same stream feeds Android and iOS alike. 🌊
よくある質問
「共有コードのFlowの基本」レッスンは無料ですか?
はい。「共有コードのFlowの基本」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「共有コードのFlowの基本」で何を学びますか?
commonMainから値のストリームを発行します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「共有コードのFlowの基本」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。