Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Type 'State<List<User>?>' has no method 'getValue(Nothing?, KProperty<*>)' and thus it cannot serve as a delegate

I'm trying to get a value from LiveData with observeAsState in jetpack compose, but I get a weird error

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

Code

@Composable
fun UserScreen(userViewModel:UserViewModel){
    val items: List<User> by userViewModel.fetchUserList.observeAsState()
    UserList(userList = items)
}

enter image description here

ViewModel

class UserViewModel: ViewModel() {

    private val dataSource = UserDataSource()
    val fetchUserList = liveData {
        emit(dataSource.dummyUserList)
    }
}
over 4 years ago · Hanz Gallego
8 Respostas
Responde à pergunta

0

You need to import

import androidx.compose.runtime.getValue

which will import this function which is basically by

inline operator fun <T> State<T>.getValue(thisObj: Any?, property: KProperty<*>): T = value
over 4 years ago · Hanz Gallego Relatório

0

You could use: import androidx.compose.runtime.*


Necessary imports are:

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.mutableStateOf

var value by remember { mutableStateOf("") }
over 4 years ago · Hanz Gallego Relatório

0

I had this problem despite imports! Took a bit but then I realised where my problem was the variable for whatever you're observing needs to be a val not a var:

In my case it was
var background: Int by currentBackgroundColor.observeAsState(0)

Should have been:
val background: Int by currentBackgroundColor.observeAsState(0)

over 4 years ago · Hanz Gallego Relatório

0

Adding a dependency fixed the problem for me:

implementation "androidx.compose.runtime:runtime:$compose_version"

Thanks to Tayaab Mizhar

over 4 years ago · Hanz Gallego Relatório

0

To fix the error add the following imports:

// for a 'val' variable
import androidx.compose.runtime.getValue

// for a `var` variable also add
import androidx.compose.runtime.setValue

// or just
import androidx.compose.runtime.*

To use a variable as a property delegate you should provide getValue operator function for read-only val variables and getValue and setValue functions for var variables.

To read more about how property delegates and state are combined in jetpack compose see Use remember to create internal state in a composable documentation section. There's also an explanation in Thinking in Compose video.

over 4 years ago · Hanz Gallego Relatório

0

In my case, in a compose application it was this missing import that provoc the error

import androidx.compose.getValue

over 4 years ago · Hanz Gallego Relatório

0

I think type of items must be nullable since you observing LiveData:

val items: List<User>? by userViewModel.fetchUserList.observeAsState()
over 4 years ago · Hanz Gallego Relatório

0

If you get a compiler error that observeAsState or getValue are not defined make sure you have the following imports:

import androidx.compose.runtime.getValue

import androidx.compose.runtime.livedata.observeAsState

This information is from Step #4 in the "Using State in Jetpack Compose" codelab.

over 4 years ago · Hanz Gallego Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda