androidkotlinandroid-lint

Expected non-nullable lint warning for LiveData in Android Studio 2020.3.1 P2


Here's the code in question:

private val _cropRequiredLiveData = SingleLiveEvent<MediaContent>()
fun crop(): LiveData<MediaContent> = _cropRequiredLiveData

private fun onDownloadComplete(content: MediaContent?) {

    if (request.isCropEnabled
        && content != null
    ) {
        _cropRequiredLiveData.value = content
        return
    }

    // ...
}

content variable in following line shows me a lint warning:

_cropRequiredLiveData.value = content

The warning is:

Expected non-nullable value

If I replace content with a content!! or requireNotNull(content), I get another lint warning saying:

Unnecessary non-null assertion (!!) on a non-null receiver of type MediaContent

Redundant 'requireNotNull' call

Is there something I can do to make the lint happy?

Here's the MediaContent file:

@Parcelize
class MediaContent(
    val metadata: MediaMetadata,
    val source: MediaSource,
    val path: String
) : Parcelable

fun MediaContent.toFile() = File(path)

Solution

  • Try this._cropRequiredLiveData.value = content. Not sure why but it works for me.