أساسيات Flow في التعليمات البرمجية المشتركة
إصدار تدفق من القيم من commonMain
أساسيات Flow في التعليمات البرمجية المشتركة درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
ماذا ستتعلم في «أساسيات Flow في التعليمات البرمجية المشتركة»؟
إصدار تدفق من القيم من commonMain تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟
لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «أساسيات Flow في التعليمات البرمجية المشتركة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟
نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أساسيات Flow في التعليمات البرمجية المشتركة
- StateFlow لحالة الواجهة المشتركة
- العوامل: map وfilter وcombine
- جمع Flows في Android وiOS