21

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?

pedja
  • 3,285
  • 5
  • 36
  • 48
  • Is this a compile time error ? What version of arch component you are using ? – ADM Dec 16 '20 at 12:11
  • Try to invalidate the cache or update the dependencies, there is no problem with your code. – Vincent Sit Dec 16 '20 at 12:13
  • @ADM yes compile time Lint error for release build only, debug build works fine. Android Studio also shows this error. Version is different for some components, lyfecycle-livedata is 2.3.0.beta01 – pedja Dec 16 '20 at 12:19
  • @VincentSit everything is up to date, invalidate cache doesnt help – pedja Dec 16 '20 at 12:19
  • 2
    Not sure if its the same but they have fixed an issue in recent release `2.3.0-alpha07` . [See this](https://developer.android.com/jetpack/androidx/releases/lifecycle#2.3.0-alpha07) . Just an advise its better to use Stable version of libraries in production. for a solution now you can ignore lint check – ADM Dec 16 '20 at 12:44
  • @ADM thank you, good to know – pedja Dec 16 '20 at 12:55

6 Answers6

46

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
    }
}
Lance Johnson
  • 1,226
  • 12
  • 8
2

This has been fixed in version 2.3.1.

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

DanielO
  • 145
  • 1
  • 12
0

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

changhao cui
  • 469
  • 4
  • 7
0

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

Sonu Sourav
  • 2,926
  • 2
  • 12
  • 25
0

Add NullSafeMutableLiveData in your function,

@Suppress("NullSafeMutableLiveData")
fun clearBeforeData(){
    _articles.postValue(null)
}
Md Tariqul Islam
  • 2,736
  • 1
  • 20
  • 35
0

I think this has been fixed in version 2.4.1

this is my working solution

private val _roomDataObserver: MutableLiveData<List<Resource<ModelClass?>>?> = MutableLiveData()
val roomDataObserver: LiveData<List<Resource<ModelClass?>>?>
    get() = _classroomDataObserver

 override fun onCleared() {
    super.onCleared()
    Log.d("VIEWMODEL", "CLEARED")
    _roomDataObserver.value = null
}
Kwesi Welbred
  • 69
  • 1
  • 5