title: Android - How to read a value from a kotlin flow?
body: <p>I have a flow of Episode  from a room database.
  I can observe as livedata this flow with no problem.</p>
  <p>But I also would like to read the last value from this flow when the user clicks on a button. I tried with first() terminal flow operator but it does not compile. Can you please help or suggest something else?</p>
  <p>The non-compiling attempt to read from a flow:</p>
  <pre><code>bd.buttonNext.setOnClickListener {
      lifecycleScope.launch {
          val episode: Episode? = viewModel.episodeFlow().first()           &lt;=== Compile ERROR
          Snackbar.make(bd.root, &quot;episode ${episode?.name}&quot;, Snackbar.LENGTH_SHORT).show()
      }
  }
  </code></pre>
  <p>This flow comes from ROOM :</p>
  <pre><code>@Query(&quot;SELECT * FROM Episode WHERE id = :id&quot;)
  fun getEpisode(id: Long): Flow&lt;Episode?&gt;
  </code></pre>
  <p>Repository:</p>
  <pre><code>fun getEpisode(episodeId: Long): Flow&lt;Episode?&gt; = dao.getEpisode(episodeId)
  </code></pre>
  <p>View model - The Id comes from a StateFlow :</p>
  <pre><code>fun episodeFlow(): Flow&lt;Episode?&gt; 
    = episodeIdStateFlow.flatMapLatest { episodeId -&gt;
      repository.getEpisode(episodeId)
    }
  </code></pre>
  <p>Compilation error :</p>
  <pre><code>Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
  public fun &lt;T&gt; Array&lt;out TypeVariable(T)&gt;.first(): TypeVariable(T) defined in kotlin.collections
  public inline fun &lt;T&gt; Array&lt;out TypeVariable(T)&gt;.first(predicate: (TypeVariable(T)) -&gt; Boolean): TypeVariable(T) defined in kotlin.collections
  public fun BooleanArray.first(): Boolean defined in kotlin.collections
  public inline fun BooleanArray.first(predicate: (Boolean) -&gt; Boolean): Boolean defined in kotlin.collections
  public fun ByteArray.first(): Byte defined in kotlin.collections
  public inline fun ByteArray.first(predicate: (Byte) -&gt; Boolean): Byte defined in kotlin.collections
  public fun CharArray.first(): Char defined in kotlin.collections
  public inline fun CharArray.first(predicate: (Char) -&gt; Boolean): Char defined in kotlin.collections
  public fun CharSequence.first(): Char defined in kotlin.text
  </code></pre>
  <p>As a &quot;workaround&quot;, I saved the episode in an instance variable like this but I would like to avoid doing it if possible :</p>
  <pre><code>var episode: Episode? = null
  ...
  viewModel.episodeFlow().asLiveData().observe(this) { episode -&gt;
     this.episode = episode
  }
  ...
  bd.buttonNext.setOnClickListener {
     Snackbar.make(bd.root, &quot;episode ${episode?.name}&quot;, Snackbar.LENGTH_SHORT).show()
  }
  </code></pre>
  <p>=================== UPDATE/SOLUTION 21/01/15 ==================</p>
  <p>Solution inspired by beigirad (see his post below) using stateIn :</p>
  <pre><code>private val _episodeSF  = episodeFlow().stateIn(viewModelScope, SharingStarted.Eagerly, null)
  val episodeSF: StateFlow&lt;Episode?&gt;
    get() = _episodeSF
  
  fun episodeFlow(): Flow&lt;Episode?&gt; = episodeIdSF.flatMapLatest { episodeId -&gt;
    repository.episodeFlow(episodeId)
  }
  </code></pre>