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

1.2K
Visualizações
Kotlin Android: Property delegate must have a 'getValue(DashViewModel, KProperty*>)' method

I am trying to follow the official Android guide for ViewModels in Kotlin. I literally copy pasted the easiest official example but the syntax seems to be illegal.

This section causes the problem:

private val users: MutableLiveData<List<User>> by lazy {
    MutableLiveData().also {
        loadUsers()
    }
}

The preview gives me this error:

Property delegate must have a 'getValue(DashViewModel, KProperty*>)' method. None of the following functions is suitable.

And if I want to launch the App I get this error:

Type inference failed: Not enough information to infer parameter T in constructor MutableLiveData<T : Any!>()
Please specify it explicitly.

I dont understand those two errors and other questions with the same error seem to have been caused by something different. My guess is that the MutableLiveData().also causes the problem but I do not know why. This is quite odd considering that this is an official example.

over 4 years ago · Hanz Gallego
1 Respostas
Responde à pergunta

0

It does not appear that you declared a User class.

The second problem is yet another documentation bug, and you need to provide the type in the MutableLiveData constructor call.

So, this works:

package com.commonsware.myapplication

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class User

class MainViewModel : ViewModel() {
  private val users: MutableLiveData<List<User>> by lazy {
    MutableLiveData<List<User>>().also {
      loadUsers()
    }
  }

  fun getUsers(): LiveData<List<User>> {
    return users
  }

  private fun loadUsers() {
    // Do an asynchronous operation to fetch users.
  }
}

This is quite odd considering that this is an official example.

In general, consider them an illustration of a technique, not necessarily something that you would copy and paste into a project.

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