Wafi_ck
Wafi_ck

Reputation: 1378

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?

Upvotes: 2

Views: 1424

Answers (1)

Francesc
Francesc

Reputation: 29330

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.

Upvotes: 2

Related Questions