title: Emit State from Firebase listener - Kotlin Flow
body: <p>I want to use Kotlin Flow to handle the FirebaseAuth State. I know that code below is wrong but I jave no idea how to fix it. I tried with <code>channelFlow</code> and it crashed always when I want <code>send</code> or <code>offer</code></p>
  <pre><code>   fun registerFlow(email: String, password: String) = flow {
      emit(AuthState.Loading)
      firebaseAuth.createUserWithEmailAndPassword(email, password)
          .addOnCompleteListener { task -&gt;
              if (task.isSuccessful) {
                  CoroutineScope(Dispatchers.IO).launch {      
                      emit(AuthState.Success(task.result?.user))
              } }else {
                    emit(AuthState.Error(task.exception))
              }
          }
  }
  </code></pre>
  <p>}</p>
  <p>the coroutine inside <code>Listener</code> throws</p>
  <pre><code>z E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
  Process: pl.rybson.musicquiz, PID: 26578
  java.lang.IllegalStateException: Flow invariant is violated:
          Emission from another coroutine is detected.
          Child of StandaloneCoroutine{Active}@4903a45, expected child of StandaloneCoroutine{Completed}@988059a.
          FlowCollector is not thread-safe and concurrent emissions are prohibited.
          To mitigate this restriction please use 'channelFlow' builder instead of 'flow'
  </code></pre>
  <p>the error when I use <code>send()</code></p>
  <pre><code>FATAL EXCEPTION: DefaultDispatcher-worker-1
  Process: pl.rybson.musicquiz, PID: 27105
  kotlinx.coroutines.channels.ClosedSendChannelException: Channel was closed
  </code></pre>