当试图从 Flow 中收集时,类型突然不匹配,它正在工作,然后突然启动。
在我的视图模型中:
class MyViewModel: ViewModel() {
lateinit var river: Flow<Int>
fun doStuff() {
river = flow {
emit(1)
}.flowOn(Dispatchers.Default)
.catch {
emit(0)
}
}
}
然后在我的活动中,我有以下内容:
lifecycleScope.launch {
viewModel.river.collect { it ->
// this whole collect is what has the error.
}
}
但collect
给出了错误:Type mismatch: inferred type is () -> Unit but FlowCollector<Int> was expected
。
这怎么可能发生?