title: Unable to Execute code after Kotlin Flow collect
body: <p>I'm trying to execute some code after calling <code>collect</code> on a <code>Flow&lt;MyClass&gt;</code>. I'm still kind of new to using Flows so I don't understand why the code after the function doesn't get called.</p>
  
  <p>How I use the Flow:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>incidentListener = FirebaseUtils.databaseReference
                        .child(AppConstants.FIREBASE_PATH_AS)
                        .child(id)
                        .listen&lt;MyClass&gt;() //This returns a Flow&lt;MyClass?&gt;?
  </code></pre>
  
  <p>How I consume the Flow:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>private suspend fun myFun() {
     viewmodel.getListener()?.collect { myClass-&gt;
         //do something here
     }
     withContext(Dispatchers.Main) { updateUI() } //the code never reaches this part
  }
  </code></pre>
  
  <p>How <code>myFun()</code> is called:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>CoroutineScope(Dispatchers.IO).launch {
     myFun()
  }
  </code></pre>
  
  <p>As far as what I've tried to make it work I've tried closing the coroutine context and it didn't work. I'm assuming Flows work differently than regular coroutines.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I'm listening through Firebase using this block of code. I don't know if it'll help but maybe the way I implemented it is causing the issue?</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>inline fun &lt;reified T&gt; Query.listen(): Flow&lt;T?&gt;? =
  callbackFlow {
      val valueListener = object : ValueEventListener {
          override fun onCancelled(databaseError: DatabaseError) {
              close()
          }
  
          override fun onDataChange(dataSnapshot: DataSnapshot) {
              try {
                  val value = dataSnapshot.getValue(T::class.java)
                  offer(value)
              } catch (exp: Exception) {
                  if (!isClosedForSend) offer(null)
              }
          }
      }
      addValueEventListener(valueListener)
      awaitClose { removeEventListener(valueListener) }
  }
  </code></pre>