title: Suspending function can only be called within coroutine body
body: <p>I'm trying to deliver realtime updates to my view with Kotlin Flows and Firebase.</p>
  
  <p>This is how I collect my realtime data from my <code>ViewModel</code>:</p>
  
  <pre><code>class MainViewModel(repo: IRepo): ViewModel() {
  
      val fetchVersionCode = liveData(Dispatchers.IO) {
          emit(Resource.Loading())
  
          try {
              repo.getVersionCode().collect {
                  emit(it)
              }
  
          } catch (e: Exception){
              emit(Resource.Failure(e))
              Log.e("ERROR:", e.message)
          }
      }
  }
  </code></pre>
  
  <p>And this is how I emit each flow of data from my repo whenever a value changes in Firebase:</p>
  
  <pre><code>class RepoImpl: IRepo {
  
      override suspend fun getVersionCodeRepo(): Flow&lt;Resource&lt;Int&gt;&gt; = flow {
  
          FirebaseFirestore.getInstance()
              .collection("params").document("app").addSnapshotListener { documentSnapshot, firebaseFirestoreException -&gt;
                  val versionCode = documentSnapshot!!.getLong("version")
                  emit(Resource.Success(versionCode!!.toInt()))
              }
      }
  </code></pre>
  
  <p>The problem is that when I use:</p>
  
  <pre><code> emit(Resource.Success(versionCode!!.toInt()))
  </code></pre>
  
  <p>Android Studio highlights the emit invocation with:</p>
  
  <blockquote>
    <p>Suspend function 'emit' should be called only from a coroutine or another suspend function</p>
  </blockquote>
  
  <p>But I'm calling this code from a <code>CoroutineScope</code> in my <code>ViewModel</code>.</p>
  
  <p>What's the problem here?</p>
  
  <p>thanks</p>