Search code examples
android-studiokotlinandroid-jetpack-composeandroid-jetpack

Type State<> has no method [Android Studio Error] Jetpack Compose


I'm facing problem when I'm trying to get value from my state

In my ViewModel

val items = MutableStateFlow<MutableList<String>>(mutableListOf())

In @Composable screen

val items: MutableList<String> by viewModel.state.items.collectAsState().value

Here I'm getting error:

Type 'MutableList' has no method 'getValue(Nothing?,KProperty<>)' and thus it cannot serve as a delegate

I tried to change val to var and add packages like below but stil the same error

import androidx.compose.runtime.*
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue

Am I missing something?


Solution

  • Replace

    val items: MutableList<String> by viewModel.state.items.collectAsState().value
    

    with

    val items: MutableList<String> by viewModel.state.items.collectAsState()
    

    or with

    val items: MutableList<String> = viewModel.state.items.collectAsState().value
    

    When using by it gets you access to the value directly.