使用mutableState'启动ViewModel的价值
我定义了一个 var Showallevent = mutableStateOf(false)
在我的ViewModel中 并想在 init {}
我的ViewModel, 但是使用时,给我这个错误: IllegalstateException:阅读在拍摄快照或尚未应用的快照中创建的状态
我如何在我的ViewModel`撰写的init
中使用我的值
@HiltViewModel
class EventPageVM @Inject constructor(
private val repository: EventRepository,
private val noteRepository: NoteRepository
) : ViewModel() {
private val _eventsList = MutableStateFlow<List<Event>>(emptyList())
val eventsList = _eventsList.asStateFlow()
///
var showAllEvent = mutableStateOf(false)
var showPastEvent = mutableStateOf(true)
init {
viewModelScope.launch(Dispatchers.IO) {
val fromTo = Extra.getFromToNextMonth(CivilDate())
val start= if (showPastEvent.value)null else fromTo[0]
val end=if (showAllEvent.value)null else fromTo[1]
repository.getEvents(start, end).distinctUntilChanged().collect { result ->
_eventsList.value = result
}
}
}
i defined avar showAllEvent = mutableStateOf(false)
in my viewmodel
and want to use it ininit {}
of my viewmodel,
but when use, give me this error:IllegalStateException: Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied
how can i use my value in init
of my viewmodel` in jetpack compose
@HiltViewModel
class EventPageVM @Inject constructor(
private val repository: EventRepository,
private val noteRepository: NoteRepository
) : ViewModel() {
private val _eventsList = MutableStateFlow<List<Event>>(emptyList())
val eventsList = _eventsList.asStateFlow()
///
var showAllEvent = mutableStateOf(false)
var showPastEvent = mutableStateOf(true)
init {
viewModelScope.launch(Dispatchers.IO) {
val fromTo = Extra.getFromToNextMonth(CivilDate())
val start= if (showPastEvent.value)null else fromTo[0]
val end=if (showAllEvent.value)null else fromTo[1]
repository.getEvents(start, end).distinctUntilChanged().collect { result ->
_eventsList.value = result
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,并选择将初始化移至JetPack撰写项目中的
。尽管在初始化块中初始化状态的方法起初似乎很有用,但在尝试运行仪器测试时,它变得很痛苦。
这是关于该主题的冗长写作: https:/ /Jetc.dev/slack/2022-05-16-mutating-state-viewmodel-constructor.html
I had the same exact issue and opted to move initialization to a
LaunchedEffect
in a Jetpack Compose project. Though the approach of initializing state in the init block seemed useful at first it became a pain when attempting to run instrumented tests.Here's a lengthy write up on the subject: https://jetc.dev/slack/2022-05-16-mutating-state-viewmodel-constructor.html
我想使用
mutableStateOf
创建的状态应从UI线程中写入/读取。当前,您使用dispatcher.io
,这意味着在背景线程中读取状态。尝试使用dispatcher.main
在与状态一起工作的代码中的上下文:viewmodelscope.launch
by默认情况下使用dispatchers.main
上下文。另一种方法是在
viewmodel
和收集 在撰写功能中。I guess states created using
mutableStateOf
should be written/read from the UI thread. Currently you useDispatchers.IO
which means that the state is read in the background thread. Try to useDispatchers.Main
context in code where you work with the state:viewModelScope.launch
by default usesDispatchers.Main
context.Another approach is to use
Flow
s in theViewModel
and collect them in the Compose functions.