title: How to collect items from a Flow until a particular condition is met?
body: <p>I have a <code>Flow&lt;List&lt;Int?&gt;&gt;</code> and I want to collect this flow but only until I get a null Int. Then the flow should get cancelled. For example,</p>
  <pre class="lang-kotlin prettyprint-override"><code>val flow = flowOf(
      listOf(1, 2),
      listOf(3, null),
      listOf(4)
  )
  flow.collectUntilNull {
      println(it)
  }
  </code></pre>
  <p>The output that I want is:</p>
  <pre><code>[1, 2]
  [3, null]
  </code></pre>
  <p>I know there is a function <code>Flow.takeWhile</code> but it doesn't emit the value where predicate returns false. In my case I want that one also.</p>
  <pre class="lang-kotlin prettyprint-override"><code>public fun &lt;T&gt; Flow&lt;T&gt;.takeWhile(predicate: suspend (T) -&gt; Boolean): Flow&lt;T&gt; = flow {
      return@flow collectWhile { value -&gt;
          if (predicate(value)) {
              emit(value)
              true
          } else {
              // I want a &quot;emit(value)&quot; here
              false
          }
      }
  }
  </code></pre>
  <p>Since <code>collectWhile</code> is internal I can't use this code. Although I guess I can copy paste that <code>collectWhile</code> implementation in my code. But is there another way to do this?</p>