如何从构建的流对象中发出值

how to emit value from built flow object

我的问题是我们如何从构建的流对象中发出一个值,如下所示:

class Sample {
    var flow: Flow<Object> = null

    fun sendRequest(){
       flow = flow{}
       requestWrapper.flowResponse = flow

    }

    fun getResponse(){
       // I want to emit data here with built flow object on 
       // requestWrapper like this

        requestWrapper.flowResponse.emit()

    }
}

这个问题有什么可能的解决方案吗?

您可以使用 MutableSharedFlow 发出值,就像您的情况一样:

class Sample {
    var flow: MutableSharedFlow<Object>? = MutableSharedFlow(...)

    fun sendRequest(){
       requestWrapper.flowResponse = flow
    }

    fun getResponse(){
       // I want to emit data here with built flow object on 
       // requestWrapper like this

        requestWrapper.flowResponse?.tryEmit(...)

    }
}