使用mutableState'启动ViewModel的价值

发布于 2025-02-07 17:48:15 字数 1008 浏览 3 评论 0原文

我定义了一个 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 a
var showAllEvent = mutableStateOf(false) in my viewmodel
and want to use it in
init {} 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一百个冬季 2025-02-14 17:48:15

我遇到了同样的问题,并选择将初始化移至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

放我走吧 2025-02-14 17:48:15

我想使用mutableStateOf创建的状态应从UI线程中写入/读取。当前,您使用dispatcher.io,这意味着在背景线程中读取状态。尝试使用dispatcher.main在与状态一起工作的代码中的上下文:

init {
    viewModelScope.launch {
        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
        }
    }
}

viewmodelscope.launch by默认情况下使用dispatchers.main上下文。

另一种方法是在viewmodel收集 在撰写功能中。

I guess states created using mutableStateOf should be written/read from the UI thread. Currently you use Dispatchers.IO which means that the state is read in the background thread. Try to use Dispatchers.Main context in code where you work with the state:

init {
    viewModelScope.launch {
        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
        }
    }
}

viewModelScope.launch by default uses Dispatchers.Main context.

Another approach is to use Flows in the ViewModel and collect them in the Compose functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文