# MutableStateFlow inits value after I set a new one but need to be vice versa

**URL:** <https://discuss.kotlinlang.org/t/mutablestateflow-inits-value-after-i-set-a-new-one-but-need-to-be-vice-versa/26994>\
**Category:** Support\
**Created:** [June 25, 2023, 7:19pm UTC](https://discuss.kotlinlang.org/t/mutablestateflow-inits-value-after-i-set-a-new-one-but-need-to-be-vice-versa/26994 "2023-06-25T19:19:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [June 25, 2023, 7:19pm UTC](https://discuss.kotlinlang.org/t/mutablestateflow-inits-value-after-i-set-a-new-one-but-need-to-be-vice-versa/26994/1 "2023-06-25T19:19:55Z")

</div>

> In Kotlin, MutableStateFlow is a type of Flow that represents a value that can be updated and observed by multiple consumers. By default, MutableStateFlow initializes its value lazily, which means that the initial value is not set until the first observer subscribes to the flow.  
> If you need to set the initial value of a MutableStateFlow before any observers subscribe to it, you can use the tryEmit method to set the value. This method attempts to set the value of the flow immediately, and returns true if the value was successfully set.

So I tried this sample:

```auto
val myFlow = MutableStateFlow("initial value")

if (!myFlow.tryEmit("new value")) {
    // Emit failed, handle error
}

```

And got `false`. Looks like I have no subscribers as for now.  
I need to set the init data only once and also tried this:

```auto
coroutineScope.launch {
  merge(myFlow, data).collect {
      with(data.first()) {
         setMyFlow(d1, d2)
      }
      cancel()
  }
}

```

Unsuccessfully. So, I’m curious how to do it correctly?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [June 25, 2023, 10:11pm UTC](https://discuss.kotlinlang.org/t/mutablestateflow-inits-value-after-i-set-a-new-one-but-need-to-be-vice-versa/26994/2 "2023-06-25T22:11:32Z")

</div>

From your description I’m not really sure what you try to achieve.

Could you provide a reproducible example in the Kotlin Playground where `tryEmit()` returns `false` for you? For me [it returns true](https://pl.kotl.in/0gIkmdhng). To be honest, I would expect `tryEmit()` for `MutableStateFlow` to always succeed, because the only thing it has to do is to replace its stored value. I looked into the code and it does exactly this - it always returns `true`: [kotlinx.coroutines/kotlinx-coroutines-core/common/src/flow/StateFlow.kt at 9c1b3afc3bc53b1e6a25a82a66337f606cbf8f2e · Kotlin/kotlinx.coroutines · GitHub](https://github.com/Kotlin/kotlinx.coroutines/blob/9c1b3afc/kotlinx-coroutines-core/common/src/flow/StateFlow.kt#L367)

Also, where did you read this quoted text? Either I misread it, or it is entirely wrong. `MutableStateFlow` doesn’t initialize lazily. Actually, it’s exactly the opposite - state flows are the only flows which we can’t create without providing an initial value.

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [June 26, 2023, 8:00am UTC](https://discuss.kotlinlang.org/t/mutablestateflow-inits-value-after-i-set-a-new-one-but-need-to-be-vice-versa/26994/3 "2023-06-26T08:00:47Z")

</div>

The quoted text is from ChatGPT. The context is next:

```auto
override fun onCreate(savedInstanceState: Bundle?) {
  viewModel.initData(data)
  setContent {
      MainScreen(viewModel)
  }
}

//...
val myFlow = MutableStateFlow(Pair(42,42))

fun initData(data: Flow<Data>){
  viewModelScope.launch {
        with(data.first()) {
           setMyFlow(d1, d2)
        }
        cancel()
  }
}

```

Found the bug: looks like data: Flow loads after compose was initialized and the value was rewritten.

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [June 26, 2023, 4:33pm UTC](https://discuss.kotlinlang.org/t/mutablestateflow-inits-value-after-i-set-a-new-one-but-need-to-be-vice-versa/26994/4 "2023-06-26T16:33:31Z")

</div>

ChatGPT is very much so not a reliable source for documentation. I’d suggest you consult the documentation for kotlinx-coroutines directly, or at the very least, paste in the documentation for Flow and MutableStateFlow into ChatGPT and then have it explain it.

---

<div class="post-metadata">

**Author:** ![reemh](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@reemh](https://discuss.kotlinlang.org/u/reemh)\
**Post date:** [January 25, 2024, 11:48am UTC](https://discuss.kotlinlang.org/t/mutablestateflow-inits-value-after-i-set-a-new-one-but-need-to-be-vice-versa/26994/5 "2024-01-25T11:48:56Z")

</div>

if you don’t care about suspeding until all your subscribers received the new emit, you just want to put a new value in your MutableStateFlow (and subscribers will get this value in the future when they will collect{} your flow) you can just do

```auto
val myFlow = MutableStateFlow("initial value")

myFlow.value = "new value"
// at this point the flow holds a new value

```
