StateFlow لحالة الواجهة المشتركة
كشف حالة قابلة للمراقبة يمكن للتطبيقين جمعها
StateFlow لحالة الواجهة المشتركة درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Kotlin Multiplatform Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
State Needs a Holder
UI state is not a one-off stream, it is a value that always exists right now. StateFlow is a Flow that holds a single current value for the screen.
Always Has a Value
Unlike a plain Flow, a StateFlow is hot and always carries a value. A new collector instantly receives the latest state, never an empty start.
MutableStateFlow to Write
You create state with MutableStateFlow, passing the initial value. This is the writable side your shared logic updates over time.
private val _count = MutableStateFlow(0)Expose a Read-Only View
Keep the mutable field private and expose it as a read-only StateFlow. The UI can observe but never reach in and mutate it directly.
val count: StateFlow<Int> = _count.asStateFlow()Update with .value
Write new state by setting .value on the mutable flow. Every collector is notified of the change right away.
_count.value = _count.value + 1Atomic update {}
For safe read-then-write changes, prefer the update function. It computes the next value from the current one without race conditions.
_count.update { current -> current + 1 }Reads Are Distinct
StateFlow only emits when the value actually changes. Setting the same value again is ignored, so the UI is not woken up for nothing.
A Single Source of Truth
Put your screen state in one StateFlow and both apps read from it. That makes your shared logic the single source of truth for the screen. ✨
Model the Whole Screen
Often the value is a data class describing the entire screen. One UiState object means the UI reads everything it needs from a single place.
data class UiState(
val loading: Boolean = false,
val items: List<String> = emptyList()
)Collect Gets the Latest
When a screen starts collecting, it immediately gets the current state, then every update after. No flicker, no missing first render.
StateFlow vs Flow
Use a plain Flow for events that come and go, and a StateFlow for state that always has a current value. UI almost always wants StateFlow.
Quick Check
One key idea to lock in.
Recap
You used StateFlow as a single source of truth: a hot stream that always holds the screen's current value, ready for both apps to observe. ✨
الأسئلة الشائعة
هل درس «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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أساسيات Flow في التعليمات البرمجية المشتركة
- StateFlow لحالة الواجهة المشتركة
- العوامل: map وfilter وcombine
- جمع Flows في Android وiOS