如何解决如何从流列表中设置 MutableStateFlow 初始值
我有一个 MutableStateFlow 和一个 Flow 变量。如何将 selectedCollection 的初始值设置为 collections 流列表的第一项?
视图模型
@Hiltviewmodel
class Homeviewmodel @Inject constructor(
private val collectionRepo: CollectionRepositoryInterface
) : viewmodel(){
var collections = collectionRepo.getCollections().asLiveData()
val selectedCollection = MutableStateFlow(//** Initial Value **//)
}
存储库界面
interface CollectionRepositoryInterface {
fun getCollections() : Flow<List<Collection>>
}
片段
viewmodel.collections.observe(viewLifecycleOwner){
collectionAdapter.submitList(it)
}
解决方法
也许你可以试试 stateIn
fun <T> Flow<T>.stateIn(
scope: CoroutineScope,started: SharingStarted,initialValue: T
): StateFlow<T> (source)
将流转换为状态流
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。