{"id":154,"date":"2026-10-18T03:38:00","date_gmt":"2026-10-18T03:38:00","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=154"},"modified":"2026-11-06T08:48:32","modified_gmt":"2026-11-06T08:48:32","slug":"kotlin-flow-explained-for-android-flow-stateflow-sharedflow","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/kotlin-flow-explained-for-android-flow-stateflow-sharedflow\/","title":{"rendered":"Kotlin Flow Explained for Android: Flow, StateFlow &#038; SharedFlow"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Modern Android applications constantly receive and process changing data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A user updates application settings.<\/li>\n\n\n\n<li>A database emits new records.<\/li>\n\n\n\n<li>A download reports its progress.<\/li>\n\n\n\n<li>A search field receives new input.<\/li>\n\n\n\n<li>A network operation returns updated information.<\/li>\n\n\n\n<li>A ViewModel updates screen state.<\/li>\n\n\n\n<li>A shopping cart total changes.<\/li>\n\n\n\n<li>A notification event occurs.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Handling these changes efficiently is essential for creating responsive Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin provides <strong>Flow<\/strong>, a coroutine-based API for working with asynchronous streams of values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Flow is widely used with Kotlin Coroutines, Room Database, ViewModel, StateFlow, SharedFlow, and Jetpack Compose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll explore Kotlin Flow from the fundamentals to practical Android development examples.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What You&#8217;ll Learn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you&#8217;ll understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Kotlin Flow is<\/li>\n\n\n\n<li>Why Android applications use Flow<\/li>\n\n\n\n<li>Flow vs suspend functions<\/li>\n\n\n\n<li>Cold and hot streams<\/li>\n\n\n\n<li>Creating Flow using <code>flow { }<\/code><\/li>\n\n\n\n<li><code>emit()<\/code> and <code>collect()<\/code><\/li>\n\n\n\n<li><code>flowOf()<\/code> and <code>asFlow()<\/code><\/li>\n\n\n\n<li>Flow operators<\/li>\n\n\n\n<li><code>map()<\/code>, <code>filter()<\/code>, and <code>take()<\/code><\/li>\n\n\n\n<li><code>onEach()<\/code><\/li>\n\n\n\n<li><code>catch()<\/code><\/li>\n\n\n\n<li><code>flowOn()<\/code><\/li>\n\n\n\n<li><code>combine()<\/code><\/li>\n\n\n\n<li><code>StateFlow<\/code><\/li>\n\n\n\n<li><code>MutableStateFlow<\/code><\/li>\n\n\n\n<li><code>SharedFlow<\/code><\/li>\n\n\n\n<li><code>MutableSharedFlow<\/code><\/li>\n\n\n\n<li>StateFlow vs SharedFlow<\/li>\n\n\n\n<li>Flow with ViewModel<\/li>\n\n\n\n<li>Flow with Jetpack Compose<\/li>\n\n\n\n<li>Common mistakes and best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s begin!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Is Kotlin Flow?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin Flow is an asynchronous data stream API built on Kotlin Coroutines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A Flow can emit multiple values over time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun getNumbers(): Flow&lt;Int&gt; = flow {\n\n    emit(10)\n\n    emit(20)\n\n    emit(30)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The Flow emits three values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n20\n30<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike a regular function that returns a single result, Flow can produce a sequence of values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Real-World Examples<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Flow is useful for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Observing Room database changes<\/li>\n\n\n\n<li>Monitoring application settings<\/li>\n\n\n\n<li>Tracking download progress<\/li>\n\n\n\n<li>Processing search queries<\/li>\n\n\n\n<li>Observing UI state<\/li>\n\n\n\n<li>Receiving live data updates<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Kotlin Flow vs Suspend Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both Flow and suspend functions work with Kotlin Coroutines, but they serve different purposes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Suspend Function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>suspend fun getUsername(): String {\n\n    delay(1000)\n\n    return \"Alex\"\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This function returns one username.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Flow Function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun getUsernames(): Flow&lt;String&gt; = flow {\n\n    emit(\"Alex\")\n\n    delay(1000)\n\n    emit(\"John\")\n\n    delay(1000)\n\n    emit(\"Emma\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This Flow emits multiple values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Comparison<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>Suspend Function<\/th><th>Flow<\/th><\/tr><tr><td>Return values<\/td><td>Usually one result<\/td><td>Multiple values<\/td><\/tr><tr><td>Asynchronous support<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Stream processing<\/td><td>Not directly<\/td><td>Yes<\/td><\/tr><tr><td>Operators<\/td><td>Not Flow operators<\/td><td>map, filter, combine, etc.<\/td><\/tr><tr><td>Typical use<\/td><td>API request<\/td><td>Observable data stream<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use Flow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use Flow when the caller needs to observe a sequence of values or updates over time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a simple one-time operation, a suspend function is often sufficient.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Add Kotlin Flow to an Android Project<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin Flow is included in the kotlinx.coroutines library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For Android development, add the appropriate coroutine dependency.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Gradle Dependency<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>dependencies {\n\n    implementation(\n        \"org.jetbrains.kotlinx:kotlinx-coroutines-android:&lt;compatible-version&gt;\"\n    )\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the placeholder with a compatible stable version.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For modern Android projects, a Gradle Version Catalog is recommended for managing dependencies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Imports<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import kotlinx.coroutines.delay\nimport kotlinx.coroutines.flow.Flow\nimport kotlinx.coroutines.flow.flow\nimport kotlinx.coroutines.flow.emitAll\nimport kotlinx.coroutines.flow.collect\nimport kotlinx.coroutines.flow.flowOf\nimport kotlinx.coroutines.flow.asFlow\nimport kotlinx.coroutines.runBlocking<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Only import the functions required by each example.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Creating Your First Kotlin Flow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create a simple Flow that emits numbers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fun numberFlow(): Flow&lt;Int&gt; = flow {\n\n    emit(1)\n\n    emit(2)\n\n    emit(3)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Collecting the Flow<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    numberFlow().collect { number -&gt;\n\n        println(number)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>flow { }<\/code> builder creates a Flow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>emit()<\/code> function sends values downstream.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>collect()<\/code> function receives emitted values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A Flow created with the standard <code>flow { }<\/code> builder is cold by default.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Its code does not execute until a collector starts collecting it.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Understanding Cold Flow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A cold Flow starts its upstream execution separately for each collector.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun coldFlow(): Flow&lt;Int&gt; = flow {\n\n    println(\"Flow started\")\n\n    emit(10)\n\n    emit(20)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    coldFlow().collect {\n        println(\"Collector 1: $it\")\n    }\n\n    coldFlow().collect {\n        println(\"Collector 2: $it\")\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Flow started\nCollector 1: 10\nCollector 1: 20\nFlow started\nCollector 2: 10\nCollector 2: 20<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each collector triggers a new execution of the cold Flow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This behavior is useful when every collector should independently execute the upstream operation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Cold Flow Sources<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>flow { }<\/code><\/li>\n\n\n\n<li><code>flowOf()<\/code><\/li>\n\n\n\n<li><code>asFlow()<\/code><\/li>\n\n\n\n<li>Many repository Flows<\/li>\n\n\n\n<li>Room DAO queries returning Flow<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. Creating Flow with flowOf()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>flowOf()<\/code> function creates a Flow from predefined values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val numbers = flowOf(\n        10,\n        20,\n        30,\n        40\n    )\n\n    numbers.collect { number -&gt;\n\n        println(number)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n20\n30\n40<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use flowOf()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use it when you already have a fixed collection of values to emit.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. Creating Flow with asFlow()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>asFlow()<\/code> extension converts supported collections, ranges, and other sources into a Flow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val numbers = listOf(\n        1,\n        2,\n        3,\n        4,\n        5\n    )\n\n    numbers.asFlow().collect {\n\n        println(it)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The list is converted into a Flow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each element is emitted to the collector.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. Emitting Values with Delays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Flow can emit values over time using suspend functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun countdownFlow(): Flow&lt;Int&gt; = flow {\n\n    for (number in 5 downTo 1) {\n\n        emit(number)\n\n        delay(1000)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    countdownFlow().collect {\n\n        println(\"Countdown: $it\")\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Countdown: 5\nCountdown: 4\nCountdown: 3\nCountdown: 2\nCountdown: 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each value is emitted approximately one second apart.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Uses<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This pattern is useful for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Countdown timers<\/li>\n\n\n\n<li>Simulated download progress<\/li>\n\n\n\n<li>Periodic status updates<\/li>\n\n\n\n<li>Step-by-step background processing<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. Kotlin Flow Operators<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Flow provides operators for transforming and processing emitted values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common operators include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>map()<\/code><\/li>\n\n\n\n<li><code>filter()<\/code><\/li>\n\n\n\n<li><code>take()<\/code><\/li>\n\n\n\n<li><code>onEach()<\/code><\/li>\n\n\n\n<li><code>catch()<\/code><\/li>\n\n\n\n<li><code>combine()<\/code><\/li>\n\n\n\n<li><code>distinctUntilChanged()<\/code><\/li>\n\n\n\n<li><code>debounce()<\/code><\/li>\n\n\n\n<li><code>flatMapLatest()<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s explore several important operators.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. Kotlin Flow map()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>map()<\/code> operator transforms each emitted value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    flowOf(1, 2, 3, 4)\n        .map { number -&gt;\n\n            number * 10\n        }\n        .collect {\n\n            println(it)\n        }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n20\n30\n40<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each number is multiplied by 10 before reaching the collector.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Uses<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use map for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Converting API models into UI models<\/li>\n\n\n\n<li>Formatting database values<\/li>\n\n\n\n<li>Transforming numeric data<\/li>\n\n\n\n<li>Mapping domain objects<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. Kotlin Flow filter()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>filter()<\/code> operator emits only values that satisfy a condition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    flowOf(1, 2, 3, 4, 5, 6)\n        .filter { number -&gt;\n\n            number % 2 == 0\n        }\n        .collect {\n\n            println(it)\n        }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>2\n4\n6<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Only even numbers pass through the filter.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Uses<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Filtering search results<\/li>\n\n\n\n<li>Selecting active users<\/li>\n\n\n\n<li>Processing valid records<\/li>\n\n\n\n<li>Ignoring irrelevant events<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12. Kotlin Flow take()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>take()<\/code> operator limits the number of emitted values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    (1..10)\n        .asFlow()\n        .take(3)\n        .collect {\n\n            println(it)\n        }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Only the first three values are collected.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the requested number of values has been received, upstream collection stops.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">13. Kotlin Flow onEach()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>onEach()<\/code> operator performs an action for every emitted value without changing the value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    flowOf(10, 20, 30)\n        .onEach { value -&gt;\n\n            println(\"Processing: $value\")\n        }\n        .collect { value -&gt;\n\n            println(\"Collected: $value\")\n        }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Processing: 10\nCollected: 10\nProcessing: 20\nCollected: 20\nProcessing: 30\nCollected: 30<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Uses<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Debug logging<\/li>\n\n\n\n<li>Observing intermediate values<\/li>\n\n\n\n<li>Performing lightweight side effects<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid putting expensive blocking work directly inside onEach when collecting on the main dispatcher.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">14. Kotlin Flow catch()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>catch()<\/code> operator handles exceptions thrown by upstream Flow operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun errorFlow(): Flow&lt;Int&gt; = flow {\n\n    emit(10)\n\n    emit(20)\n\n    throw IllegalStateException(\n        \"Something went wrong\"\n    )\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Collecting with catch()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    errorFlow()\n        .catch { exception -&gt;\n\n            println(\n                \"Error: ${exception.message}\"\n            )\n        }\n        .collect {\n\n            println(it)\n        }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>10\n20\nError: Something went wrong<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The catch operator handles upstream exceptions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It does not automatically catch exceptions thrown inside the downstream collect block.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, coroutine cancellation should not be converted into an ordinary application error.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">15. Kotlin Flow flowOn()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>flowOn()<\/code> operator changes the execution context of upstream Flow operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun calculateFlow(): Flow&lt;Int&gt; = flow {\n\n    for (number in 1..5) {\n\n        emit(number * number)\n    }\n\n}.flowOn(Dispatchers.Default)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    calculateFlow().collect {\n\n        println(it)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n4\n9\n16\n25<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The upstream calculation runs using Dispatchers.Default.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The downstream collector continues in its own coroutine context.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use flowOn for appropriate upstream execution context changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid manually switching context inside a standard flow builder to emit from another coroutine context.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">16. Kotlin Flow combine()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>combine()<\/code> operator combines the latest values from multiple Flows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val firstName = flowOf(\"Alex\")\n\n    val lastName = flowOf(\"Johnson\")\n\n    combine(\n        firstName,\n        lastName\n    ) { first, last -&gt;\n\n        \"$first $last\"\n\n    }.collect {\n\n        println(it)\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Alex Johnson<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Uses<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">combine is useful for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Combining user preferences<\/li>\n\n\n\n<li>Calculating form validity<\/li>\n\n\n\n<li>Combining database results<\/li>\n\n\n\n<li>Building dashboard state<\/li>\n\n\n\n<li>Updating derived UI information<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">17. Understanding Hot Flow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike a typical cold Flow, a hot Flow exists independently of individual collectors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin provides hot stream APIs including:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>StateFlow<\/li>\n\n\n\n<li>SharedFlow<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Cold vs Hot Flow<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>Cold Flow<\/th><th>Hot Flow<\/th><\/tr><tr><td>Starts per collector<\/td><td>Usually yes<\/td><td>No<\/td><\/tr><tr><td>Shares emissions<\/td><td>Not by default<\/td><td>Yes<\/td><\/tr><tr><td>Maintains current state<\/td><td>Not necessarily<\/td><td>StateFlow does<\/td><\/tr><tr><td>Multiple collectors<\/td><td>Separate upstream execution<\/td><td>Can observe shared source<\/td><\/tr><tr><td>Typical use<\/td><td>Data pipeline<\/td><td>UI state and events<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow and SharedFlow have different replay and state semantics.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They should not be treated as interchangeable.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">18. What Is StateFlow?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is a hot Flow that represents observable state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It always has a current value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val counter = MutableStateFlow(0)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The initial value is 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can update it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>counter.value = 10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reading the Value<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>println(counter.value)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use StateFlow?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is useful for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Screen UI state<\/li>\n\n\n\n<li>Loading indicators<\/li>\n\n\n\n<li>User profile state<\/li>\n\n\n\n<li>Selected filters<\/li>\n\n\n\n<li>Form state<\/li>\n\n\n\n<li>Application settings<\/li>\n\n\n\n<li>Dashboard information<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">19. MutableStateFlow vs StateFlow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MutableStateFlow allows state updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow provides a read-only interface for observing state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class CounterViewModel : ViewModel() {\n\n    private val _count = MutableStateFlow(0)\n\n    val count: StateFlow&lt;Int&gt; =\n        _count.asStateFlow()\n\n    fun increment() {\n\n        _count.value += 1\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel owns the mutable state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Other components can observe the read-only StateFlow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Expose StateFlow publicly and keep MutableStateFlow private.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This prevents external components from changing ViewModel state directly.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">20. StateFlow in Jetpack Compose<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose can observe StateFlow and update the UI automatically.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ViewModel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class CounterViewModel : ViewModel() {\n\n    private val _count = MutableStateFlow(0)\n\n    val count: StateFlow&lt;Int&gt; =\n        _count.asStateFlow()\n\n    fun increment() {\n\n        _count.value += 1\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Compose Screen<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun CounterScreen(\n    viewModel: CounterViewModel = viewModel()\n) {\n\n    val count by viewModel.count\n        .collectAsStateWithLifecycle()\n\n    Column(\n        modifier = Modifier\n            .fillMaxSize()\n            .padding(24.dp),\n        verticalArrangement = Arrangement.Center,\n        horizontalAlignment = Alignment.CenterHorizontally\n    ) {\n\n        Text(\n            text = \"Counter: $count\",\n            style = MaterialTheme.typography.headlineMedium\n        )\n\n        Spacer(\n            modifier = Modifier.height(16.dp)\n        )\n\n        Button(\n            onClick = {\n                viewModel.increment()\n            }\n        ) {\n\n            Text(\"Increment\")\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Compose screen collects StateFlow using collectAsStateWithLifecycle().<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the count changes, Compose updates the displayed value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Use collectAsStateWithLifecycle()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It integrates Flow collection with Android lifecycle awareness.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For Android screens, this is generally preferred over collecting StateFlow without lifecycle awareness.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">21. What Is SharedFlow?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SharedFlow is a hot Flow designed for broadcasting values to multiple collectors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike StateFlow, SharedFlow does not require an initial value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>val events = MutableSharedFlow&lt;String&gt;()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Emitting an Event<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>events.emit(\"User logged in\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Collecting Events<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>events.collect { event -&gt;\n\n    println(event)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Uses<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SharedFlow can be useful for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Application-wide event streams<\/li>\n\n\n\n<li>Broadcasting updates<\/li>\n\n\n\n<li>Notification events<\/li>\n\n\n\n<li>Shared signals between components<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A default MutableSharedFlow has no replay cache.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Values emitted when there are no subscribers are not retained for future collectors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This behavior matters when using SharedFlow for one-time UI events.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">22. MutableSharedFlow Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create a simple event stream.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val events = MutableSharedFlow&lt;String&gt;()\n\n    val collectorJob = launch {\n\n        events.collect { event -&gt;\n\n            println(\"Received: $event\")\n        }\n    }\n\n    collectorJob.start()\n\n    events.subscriptionCount.first {\n        it &gt; 0\n    }\n\n    events.emit(\"Download started\")\n\n    events.emit(\"Download completed\")\n\n    collectorJob.cancelAndJoin()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Received: Download started\nReceived: Download completed<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The collector starts observing SharedFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We wait until a subscriber is registered.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then we emit two events.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both events are received by the active collector.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike a cold Flow, a SharedFlow does not complete automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Collectors normally continue until their coroutine is cancelled.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">23. StateFlow vs SharedFlow<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both are hot Flows, but they serve different purposes.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>StateFlow<\/th><th>SharedFlow<\/th><\/tr><tr><td>Initial value required<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Current value available<\/td><td>Yes<\/td><td>Not through a value property<\/td><\/tr><tr><td>Replays latest value<\/td><td>Yes<\/td><td>Configurable<\/td><\/tr><tr><td>Duplicate equal values<\/td><td>Conflated<\/td><td>Depends on configuration<\/td><\/tr><tr><td>Main purpose<\/td><td>Observable state<\/td><td>Broadcast stream<\/td><\/tr><tr><td>Typical Android use<\/td><td>Screen state<\/td><td>Shared events<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Use StateFlow For<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Loading state<\/li>\n\n\n\n<li>User information<\/li>\n\n\n\n<li>Selected tab<\/li>\n\n\n\n<li>Form state<\/li>\n\n\n\n<li>Screen data<\/li>\n\n\n\n<li>Settings state<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Use SharedFlow For<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Broadcasting events<\/li>\n\n\n\n<li>Shared notifications<\/li>\n\n\n\n<li>Multiple subscribers receiving updates<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For critical one-time UI actions, consider whether a durable state-based approach or event queue is more appropriate.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SharedFlow does not guarantee delivery to collectors that are not active unless replay is configured, and replay can introduce duplicate handling.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">24. Practical Android Example: User Profile Repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create a repository that emits a user profile using Flow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create User Model<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class User(\n    val id: Int,\n    val name: String,\n    val email: String\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create Repository<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserRepository {\n\n    fun getUser(): Flow&lt;User&gt; = flow {\n\n        delay(1500)\n\n        emit(\n            User(\n                id = 101,\n                name = \"Alex\",\n                email = \"alex@example.com\"\n            )\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The repository returns Flow&lt;User&gt;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It simulates a delay and then emits a User object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a real Android application, this could represent database updates or a sequence of network-driven updates.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">25. Create UI State<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A modern Android screen should represent loading, success, and error states explicitly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sealed interface UserUiState {\n\n    data object Loading : UserUiState\n\n    data class Success(\n        val user: User\n    ) : UserUiState\n\n    data class Error(\n        val message: String\n    ) : UserUiState\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The UI can display different content based on the current state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This pattern works well with ViewModel, StateFlow, and Jetpack Compose.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">26. Create ViewModel with Flow and StateFlow<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserViewModel : ViewModel() {\n\n    private val repository = UserRepository()\n\n    private val _uiState =\n        MutableStateFlow&lt;UserUiState&gt;(\n            UserUiState.Loading\n        )\n\n    val uiState: StateFlow&lt;UserUiState&gt; =\n        _uiState.asStateFlow()\n\n    init {\n        loadUser()\n    }\n\n    fun loadUser() {\n\n        viewModelScope.launch {\n\n            _uiState.value = UserUiState.Loading\n\n            repository.getUser()\n                .catch { exception -&gt;\n\n                    _uiState.value =\n                        UserUiState.Error(\n                            \"Unable to load user\"\n                        )\n                }\n                .collect { user -&gt;\n\n                    _uiState.value =\n                        UserUiState.Success(user)\n                }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel collects the repository Flow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When data arrives, it updates StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Compose screen observes the updated state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Production Note<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The example keeps the architecture simple for learning.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a production application, consider dependency injection, appropriate error classification, cancellation of previous requests, and unit testing.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">27. Jetpack Compose User Profile Screen<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s display the user profile using Material 3.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun UserProfileScreen(\n    viewModel: UserViewModel = viewModel()\n) {\n\n    val uiState by viewModel.uiState\n        .collectAsStateWithLifecycle()\n\n    Column(\n        modifier = Modifier\n            .fillMaxSize()\n            .padding(24.dp),\n        verticalArrangement = Arrangement.Center,\n        horizontalAlignment = Alignment.CenterHorizontally\n    ) {\n\n        when (val state = uiState) {\n\n            UserUiState.Loading -&gt; {\n\n                CircularProgressIndicator()\n\n                Spacer(\n                    modifier = Modifier.height(16.dp)\n                )\n\n                Text(\"Loading user...\")\n            }\n\n            is UserUiState.Success -&gt; {\n\n                Text(\n                    text = state.user.name,\n                    style = MaterialTheme.typography.headlineMedium\n                )\n\n                Spacer(\n                    modifier = Modifier.height(8.dp)\n                )\n\n                Text(state.user.email)\n\n                Spacer(\n                    modifier = Modifier.height(16.dp)\n                )\n\n                Button(\n                    onClick = {\n                        viewModel.loadUser()\n                    }\n                ) {\n\n                    Text(\"Reload\")\n                }\n            }\n\n            is UserUiState.Error -&gt; {\n\n                Text(\n                    text = state.message,\n                    color = MaterialTheme.colorScheme.error\n                )\n\n                Spacer(\n                    modifier = Modifier.height(16.dp)\n                )\n\n                Button(\n                    onClick = {\n                        viewModel.loadUser()\n                    }\n                ) {\n\n                    Text(\"Retry\")\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Required Imports<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import androidx.compose.foundation.layout.Arrangement\nimport androidx.compose.foundation.layout.Column\nimport androidx.compose.foundation.layout.Spacer\nimport androidx.compose.foundation.layout.fillMaxSize\nimport androidx.compose.foundation.layout.height\nimport androidx.compose.foundation.layout.padding\nimport androidx.compose.material3.Button\nimport androidx.compose.material3.CircularProgressIndicator\nimport androidx.compose.material3.MaterialTheme\nimport androidx.compose.material3.Text\nimport androidx.compose.runtime.Composable\nimport androidx.compose.runtime.getValue\nimport androidx.compose.ui.Alignment\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.unit.dp\nimport androidx.lifecycle.ViewModel\nimport androidx.lifecycle.viewModelScope\nimport androidx.lifecycle.viewmodel.compose.viewModel\nimport androidx.lifecycle.compose.collectAsStateWithLifecycle\nimport kotlinx.coroutines.delay\nimport kotlinx.coroutines.flow.Flow\nimport kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.StateFlow\nimport kotlinx.coroutines.flow.asStateFlow\nimport kotlinx.coroutines.flow.catch\nimport kotlinx.coroutines.flow.collect\nimport kotlinx.coroutines.flow.flow\nimport kotlinx.coroutines.launch<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Expected UI \u2014 Loading<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|                                |\n|          Loading...            |\n|                                |\n|       Loading user...          |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Expected UI \u2014 Success<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|                                |\n|             Alex               |\n|                                |\n|       alex@example.com         |\n|                                |\n|          &#91; Reload ]            |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Expected UI \u2014 Error<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|                                |\n|      Unable to load user       |\n|                                |\n|           &#91; Retry ]            |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How It Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Repository<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Produces a Flow containing user information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ViewModel<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Collects the Flow and updates StateFlow.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>StateFlow<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Exposes observable UI state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Jetpack Compose<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Observes state changes and updates the screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Retry Button<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Starts another loading operation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Repeated Reload taps can create overlapping collectors in this simplified example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production applications, cancel the previous loading Job, disable repeated requests while loading, or use a reactive trigger with flatMapLatest.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">28. Kotlin Flow with Room Database<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">One of the most common uses of Flow in Android is observing Room database changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example DAO<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Dao\ninterface UserDao {\n\n    @Query(\"SELECT * FROM users\")\n    fun observeUsers(): Flow&lt;List&lt;UserEntity&gt;&gt;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Room returns a Flow of user records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the observed database tables change, Room can emit updated query results.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Repository Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserRepository(\n    private val userDao: UserDao\n) {\n\n    fun observeUsers(): Flow&lt;List&lt;UserEntity&gt;&gt; {\n\n        return userDao.observeUsers()\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Why Is This Useful?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The UI can react automatically to database updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>User adds a record.<\/li>\n\n\n\n<li>Room database updates.<\/li>\n\n\n\n<li>Flow emits the new query result.<\/li>\n\n\n\n<li>ViewModel receives updated data.<\/li>\n\n\n\n<li>Jetpack Compose displays the latest information.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This creates a reactive data pipeline.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">29. Common Kotlin Flow Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Forgetting to Collect a Cold Flow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Creating a cold Flow does not execute its producer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You must collect it or use another terminal operation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Collecting Flow Without Lifecycle Awareness<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">On Android screens, prefer lifecycle-aware collection such as collectAsStateWithLifecycle().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Exposing MutableStateFlow Publicly<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Keep mutable state private inside the ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Expose StateFlow instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Using SharedFlow for Every UI State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is usually more appropriate for continuously observable screen state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Ignoring Cancellation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Flow collection follows coroutine cancellation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid swallowing cancellation exceptions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Performing Blocking Work on the Main Thread<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use appropriate dispatchers for expensive upstream operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Launching Multiple Unmanaged Collectors<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Repeatedly starting collectors can cause duplicate work and unexpected updates.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 8: Assuming SharedFlow Stores Every Event<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SharedFlow replay and buffering depend on its configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Events can be missed when no subscriber is active.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">30. Kotlin Flow Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Follow these recommendations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use Flow for observable asynchronous streams.<\/li>\n\n\n\n<li>Prefer suspend functions for simple one-time operations.<\/li>\n\n\n\n<li>Use StateFlow for screen UI state.<\/li>\n\n\n\n<li>Use SharedFlow when broadcasting events is appropriate.<\/li>\n\n\n\n<li>Keep MutableStateFlow private.<\/li>\n\n\n\n<li>Use collectAsStateWithLifecycle() in Android Compose screens.<\/li>\n\n\n\n<li>Handle errors at meaningful boundaries.<\/li>\n\n\n\n<li>Preserve coroutine cancellation.<\/li>\n\n\n\n<li>Use flowOn() for appropriate upstream dispatcher changes.<\/li>\n\n\n\n<li>Avoid unnecessary collectors.<\/li>\n\n\n\n<li>Use structured concurrency.<\/li>\n\n\n\n<li>Prefer immutable UI state models.<\/li>\n\n\n\n<li>Test Flow emissions and cancellation behavior.<\/li>\n\n\n\n<li>Use Room Flow queries for reactive database updates.<\/li>\n\n\n\n<li>Keep UI and data layers separate.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These practices help build scalable and maintainable Android applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">31. Kotlin Flow Quick Reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Concept<\/th><th>Purpose<\/th><\/tr><tr><td>Flow<\/td><td>Asynchronous stream of values<\/td><\/tr><tr><td>flow { }<\/td><td>Create a cold Flow<\/td><\/tr><tr><td>emit()<\/td><td>Emit a value<\/td><\/tr><tr><td>collect()<\/td><td>Receive emitted values<\/td><\/tr><tr><td>flowOf()<\/td><td>Create Flow from values<\/td><\/tr><tr><td>asFlow()<\/td><td>Convert supported sources to Flow<\/td><\/tr><tr><td>map()<\/td><td>Transform values<\/td><\/tr><tr><td>filter()<\/td><td>Filter values<\/td><\/tr><tr><td>take()<\/td><td>Limit emissions<\/td><\/tr><tr><td>onEach()<\/td><td>Perform an action per emission<\/td><\/tr><tr><td>catch()<\/td><td>Handle upstream exceptions<\/td><\/tr><tr><td>flowOn()<\/td><td>Change upstream execution context<\/td><\/tr><tr><td>combine()<\/td><td>Combine latest values<\/td><\/tr><tr><td>StateFlow<\/td><td>Observable state holder<\/td><\/tr><tr><td>MutableStateFlow<\/td><td>Mutable observable state<\/td><\/tr><tr><td>SharedFlow<\/td><td>Hot broadcast stream<\/td><\/tr><tr><td>MutableSharedFlow<\/td><td>Mutable shared stream<\/td><\/tr><tr><td>collectAsStateWithLifecycle()<\/td><td>Lifecycle-aware Compose collection<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">32. Practical Exercises<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Try these exercises to improve your Flow skills.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 1: Basic Flow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a Flow that emits numbers from 1 to 10.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 2: Delayed Emissions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Emit five messages with a one-second delay between each.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 3: map()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Transform numbers into their squares.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 4: filter()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Filter even numbers from a Flow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 5: take()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Collect only the first three values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 6: catch()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a Flow that throws an exception and handle it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 7: StateFlow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a counter ViewModel using MutableStateFlow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 8: SharedFlow<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create an event stream that broadcasts download status messages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 9: Jetpack Compose<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build a screen that observes StateFlow and updates automatically.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 10: Room Integration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a Room DAO that returns Flow&lt;List&lt;Entity&gt;&gt;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Observe database changes in a ViewModel.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">33. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is Kotlin Flow?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin Flow is a coroutine-based API for representing asynchronous streams of values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between Flow and StateFlow?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Flow is a general stream abstraction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow is a hot Flow that always holds a current value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between StateFlow and SharedFlow?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow represents observable state with a current value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">SharedFlow broadcasts values and supports configurable replay and buffering.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Is Kotlin Flow cold or hot?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Flow created using standard builders such as flow { } is typically cold.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow and SharedFlow are hot.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use Flow with Jetpack Compose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Flow is commonly used with ViewModel, StateFlow, and collectAsStateWithLifecycle().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does Flow replace LiveData?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">StateFlow and Flow are widely used alternatives to LiveData in modern Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, LiveData remains supported and can still be appropriate in existing projects.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can Room return Flow?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Room DAO queries can return Flow to observe database changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can Flow emit multiple values?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. A Flow can emit multiple values over time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I use SharedFlow for navigation events?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SharedFlow can be used for events, but developers should carefully consider lifecycle behavior and potential event loss.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For important navigation or user-action outcomes, a state-based approach may be more reliable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the best way to collect StateFlow in Compose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For Android applications, collectAsStateWithLifecycle() is generally recommended.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What We&#8217;ve Learned<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kotlin Flow fundamentals<\/li>\n\n\n\n<li>Flow vs suspend functions<\/li>\n\n\n\n<li>Cold and hot streams<\/li>\n\n\n\n<li>flow builder<\/li>\n\n\n\n<li>emit and collect<\/li>\n\n\n\n<li>flowOf and asFlow<\/li>\n\n\n\n<li>Flow operators<\/li>\n\n\n\n<li>map, filter, and take<\/li>\n\n\n\n<li>onEach and catch<\/li>\n\n\n\n<li>flowOn and combine<\/li>\n\n\n\n<li>StateFlow<\/li>\n\n\n\n<li>MutableStateFlow<\/li>\n\n\n\n<li>SharedFlow<\/li>\n\n\n\n<li>MutableSharedFlow<\/li>\n\n\n\n<li>StateFlow vs SharedFlow<\/li>\n\n\n\n<li>ViewModel integration<\/li>\n\n\n\n<li>Jetpack Compose integration<\/li>\n\n\n\n<li>Room database integration<\/li>\n\n\n\n<li>Common mistakes<\/li>\n\n\n\n<li>Best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Kotlin Flow is an essential tool for building reactive Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining Flow, StateFlow, ViewModel, and Jetpack Compose, developers can build applications that respond efficiently to changing data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding these concepts provides a strong foundation for advanced Android architecture, database integration, networking, and modern UI development.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In our next tutorial, we&#8217;ll explore:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Kotlin Flow Operators Explained: map, filter, combine, debounce &amp; flatMapLatest<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Flow transformation operators<\/li>\n\n\n\n<li>map and mapLatest<\/li>\n\n\n\n<li>filter<\/li>\n\n\n\n<li>distinctUntilChanged<\/li>\n\n\n\n<li>debounce<\/li>\n\n\n\n<li>combine<\/li>\n\n\n\n<li>zip<\/li>\n\n\n\n<li>flatMapConcat<\/li>\n\n\n\n<li>flatMapMerge<\/li>\n\n\n\n<li>flatMapLatest<\/li>\n\n\n\n<li>collectLatest<\/li>\n\n\n\n<li>Search-as-you-type implementation<\/li>\n\n\n\n<li>Practical Android and Jetpack Compose examples<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Happy Coding!<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continue learning modern Android development with <strong>Moiks Developer Blog<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn Kotlin Flow step by step with examples covering cold and hot streams, StateFlow, SharedFlow, Flow operators, error handling, ViewModel, and Jetpack Compose.<\/p>\n","protected":false},"author":1,"featured_media":155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[3,333,7],"tags":[],"class_list":["post-154","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-development","category-android-tutorials","category-coroutines-flow"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=154"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"predecessor-version":[{"id":156,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/154\/revisions\/156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/155"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}