0Pricing
Kotlin Multiplatform Academy · 课时

共享代码中的 Flow 基础

从 commonMain 发出值流

共享代码中的 Flow 基础 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。

「共享代码中的 Flow 基础」这节课中我会学到什么?

从 commonMain 发出值流 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Kotlin Multiplatform Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「共享代码中的 Flow 基础」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?

能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 共享代码中的 Flow 基础
  2. 用于共享界面状态的 StateFlow
  3. 运算符:map、filter、combine
  4. 在 Android 和 iOS 中收集 Flow
← 返回 Kotlin Multiplatform Academy