Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

344
Vistas
Cannot set non-nullable LiveData value to null

The error from the title is returned for the following code, which makes no sense

private val _error = MutableLiveData<String?>()
val error: LiveData<String?> get() = _error


_error.postValue(null)  //Error Cannot set non-nullable LiveData value to null [NullSafeMutableLiveData]

parameter String of _error is obviously nullable, am I doing something wrong?

over 4 years ago · Hanz Gallego
4 Respuestas
Responde la pregunta

0

This has been fixed in version 2.3.1.

https://developer.android.com/jetpack/androidx/releases/lifecycle#version_231_2

over 4 years ago · Hanz Gallego Denunciar

0

This appears to be related to a bug already reported against androidx.lifecycle pre-release of 2.3.0 https://issuetracker.google.com/issues/169249668.

Workarounds I have found:

  1. turn off or reduce severity of NullSafeMutableLiveData in

build.gradle

android {
  ...
  lintOptions {
    disable 'NullSafeMutableLiveData'
  }
}

or lint.xml in root dir

<?xml version="1.0" encoding="UTF-8"?>
<lint>
    <issue id="NullSafeMutableLiveData" severity="warning" />
</lint>
  1. Do the work for MutableLiveData encapsulation via backing properties dance (which really hurts my eyes).
class ExampleViewModel : ViewModel() {

    private val _data1 = MutableLiveData<Int>()
    val data1: LiveData<Int> = _data1

    private val _data2 = MutableLiveData<Int?>()
    val data2: LiveData<Int?> = _data2

    fun funct() {
        _data1.value = 1
        _data2.value = null
    }
}
over 4 years ago · Hanz Gallego Denunciar

0

It seems fixed in lifecycle 2.4.0-SNAPSHOT. Please wait for the official release.

over 4 years ago · Hanz Gallego Denunciar

0

This issue was coming in 2.3.0 also but after upgrading to 2.4.0 it is not coming.

over 4 years ago · Hanz Gallego Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda