类型不匹配推断的类型为()->单位,但需要FlowCollector<Int> [英] Type mismatch inferred type is () -> Unit but FlowCollector<Int> was expected

查看:16
本文介绍了类型不匹配推断的类型为()->单位,但需要FlowCollector<Int>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试从流中收集时,类型突然不匹配,它正在工作,然后突然启动。

在我的视图模型中:

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

这怎么可能发生?

推荐答案

您可能正在使用the direct collect() function on Flow

对于您的语法,您需要importthe collect() extension function

(我真的希望他们不要给它们起同样的名字...)

这篇关于类型不匹配推断的类型为()-&gt;单位,但需要FlowCollector&lt;Int&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆