''awaitClose { yourCallbackOrListener. cancel() }' should be used in the end of callbackFlow block

How to properly use callBacFlow of Coroutie with Firebase i am working on image processig app in which i am converting task class to flow api callback

         //Callback for this 
           fun requestDetectInImageFlow(firebaseVisionImage: FirebaseVisionImage, imageProxy: ImageProxy): Flow<String> =
    
    callbackFlow {
     detector.processImage(firebaseVisionImage).addOnSuccessListener {
            imageProxy.close()

            if(it!=null) {
                this.offer(it.text) }

        }.addOnFailureListener {
            imageProxy.close()
         this.close(it) }


awaitClose {
    detector.close() }

    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source