Kotlin Multiplatform Academy · درس

كشف حالة الواجهة باعتبارها StateFlow

نشر مصدر واحد للحقيقة للشاشة

الدرس 2 من 413 خطوة

كشف حالة الواجهة باعتبارها StateFlow درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

One Source of Truth

Your screen needs a single, reliable picture of its data. A StateFlow gives you exactly that: one observable value the UI can trust.

Model the Screen as State

Bundle everything the screen shows into one immutable data class. Loading, data and errors all live together in a single object.

data class HomeState(
    val isLoading: Boolean = false,
    val name: String = ""
)

Keep a Private MutableStateFlow

Inside the ViewModel hold a private MutableStateFlow. Only your code may change it, so state stays controlled and predictable.

private val _state =
    MutableStateFlow(HomeState())

Expose a Read-Only View

Publish it as a read-only StateFlow. The UI can collect values but can never write to it directly. Encapsulation, kept clean.

val state: StateFlow<HomeState> =
    _state.asStateFlow()

It Always Has a Value

Unlike a plain Flow, a StateFlow always holds a current value. New collectors instantly get the latest state, no waiting.

Update with .update

Change state safely using update. It copies the current value and applies your change atomically, even across threads.

_state.update { it.copy(isLoading = true) }

copy Keeps It Immutable

Because the state is a data class, copy produces a fresh object with one field changed. Old state is never mutated in place.

Load Data, Then Emit

Launch a coroutine, fetch your data, then emit the new state. The UI re-renders automatically when the value changes. ✨

scope.launch {
    val name = repo.greet()
    _state.update { it.copy(name = name) }
}

StateFlow vs LiveData

Android devs know LiveData, but it is JVM-only. StateFlow works on every KMP target, so it is the portable choice.

Read the Value Directly

Need the current state without collecting? Just read .value. Handy for tests or one-off checks inside the ViewModel.

val current = _state.value

The UI Stays Dumb

The screen only collects state and draws it. All decisions stay in shared code, so both apps behave identically.

Quick Check

Let's confirm why we split mutable and read-only flows.

Recap

Bundle screen data in a state class, hold it in a private MutableStateFlow, and expose a read-only StateFlow the UI just observes. 🚀

البدء مجانًا

تعلم Kotlin مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «كشف حالة الواجهة باعتبارها StateFlow» مجاني؟

نعم — نص درس «كشف حالة الواجهة باعتبارها StateFlow» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «كشف حالة الواجهة باعتبارها StateFlow»؟

نشر مصدر واحد للحقيقة للشاشة تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «كشف حالة الواجهة باعتبارها StateFlow»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. قاعدة ViewModel متعددة المنصات
  2. كشف حالة الواجهة باعتبارها StateFlow
  3. معالجة نوايا المستخدم وإجراءاته
  4. ربط ViewModel بكل واجهة
← العودة إلى Kotlin Multiplatform Academy