how do I cancel a flow after the first time `colle...
# coroutines
a
how do I cancel a flow after the first time
collect { }
completes? I’d like to do something like this, but the job val is not visible:
Copy code
val eventJob = launch {
   myChannel
      .consumeAsFlow()
      .filterAllButFinalEvent()
      .collect { event ->
          doSomething()
          eventJob.cancel() // doesn’t compile
      }
   }
a
a
ah - I see. So do I put that after
.consumeAsFlow()
or in place of it?
a
Copy code
launch {
    val firstEvent = yourFlow.first()
    doSomething()
}
Or alternatively you can use
Flow.take(1).collect()
.
a
perfect - that’s done it. Many thanks @Albert Chang 👍