Status Update
Comments
sp...@gmail.com <sp...@gmail.com> #2
Changing val data1 = MutableLiveData<Int>()
to val data1 = MutableLiveData<Int?>()
fixes this error, BTW.
se...@google.com <se...@google.com>
la...@gmail.com <la...@gmail.com> #3
This bit me today and I was about to submit a bug but seems like it is in the queue. Given I already did the work, I figured I would share a sample git repo w/ the problem
Workarounds I have found:
- turn off or reduce severity of
NullSafeMutableLiveData
in
build.gradle
android {
...
lintOptions {
// Turns off checks for the issue IDs you specify.
disable 'NullSafeMutableLiveData'
}
}
or lint.xml
in root dir
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="NullSafeMutableLiveData" severity="warning" />
</lint>
- 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
}
}
ma...@gmail.com <ma...@gmail.com> #4
I can confirm this made to a "stable" version 2.3.0
despite being known in early alpha versions...
sp...@gmail.com <sp...@gmail.com> #5
It already existed in 2.2.0.
[Deleted User] <[Deleted User]> #6
ap...@google.com <ap...@google.com> #7
Branch: androidx-main
commit d5adf6168357145df07976060f0ae88dfdb05fe8
Author: Jeremy Woods <jbwoods@google.com>
Date: Mon Mar 08 14:17:49 2021
Fix MutableLiveData lint rule false report
If using multiple MutableLiveData variables as fields and mixed the
nullability, the lint rule could falsely report that a livedata value
was improperly being made null. This is because as constructed, it
didn't match up the fields with the assignments.
This change maps the field variable names to the reference types to
ensure that when reporting, the rule knows how to match them.
This also cleans up the rule by changing it to using a UastScanner.
Relnote: "The `NonNullableMutableLiveData` lint rule can now properly
differentiate between field variables with different nullability."
Test: added nullLiterallFailMultipleFieldsDifferent
Bug: 169249668
Change-Id: Ieb0270de1b22f3a10641643438bc2b9f1f5d72e8
M lifecycle/lifecycle-livedata-core-ktx-lint/src/main/java/androidx/lifecycle/lint/NonNullableMutableLiveDataDetector.kt
M lifecycle/lifecycle-livedata-core-ktx-lint/src/test/java/androidx/lifecycle/lint/NonNullableMutableLiveDataDetectorTest.kt
jb...@google.com <jb...@google.com> #8
This has been addressed internally and will be in the next lifecycle release.
You can follow the 7201697
with the lifecycle-livedata-core-ktx:2.4.0-SNAPSHOT
dependency to verify.
sp...@gmail.com <sp...@gmail.com> #9
Awesome! Thank you for fixing this.
jb...@google.com <jb...@google.com>
go...@gmail.com <go...@gmail.com> #10
n....@tinkoff.ru <n....@tinkoff.ru> #11
Stack: ArrayIndexOutOfBoundsException:NonNullableMutableLiveDataDetector$createUastHandler$1.getFieldTypeReference(NonNullableMutableLiveDataDetector.kt:101)←NonNullableMutableLiveDataDetector$createUastHandler$1.visitClass(NonNullableMutableLiveDataDetector.kt:88)←UElementVisitor$DispatchPsiVisitor.visitClass(UElementVisitor.kt:602)←UElementVisitor$DelegatingPsiVisitor.visitClass(UElementVisitor.kt:1153)←AbstractKotlinUClass.accept(KotlinUClass.kt:63)←ImplementationUtilsKt.acceptList(implementationUtils.kt:23)←UFile$DefaultImpls.accept(UFile.kt:83)←KotlinUFile.accept(KotlinUFile.kt:30)
an...@gmail.com <an...@gmail.com> #12
cu...@gmail.com <cu...@gmail.com> #13
val currentUserId = MutableLiveData<String>()
fun setUserID(userId: String?) {
if (userId == null) {
return
}
val check: String = userId // no error
currentUserId.value = userId // error: Expected non-nullable value
}
cu...@gmail.com <cu...@gmail.com> #14
jo...@gmail.com <jo...@gmail.com> #15
ya...@gmail.com <ya...@gmail.com> #16
It still seems present in 2.4.0-alpha02:
Error: Cannot set non-nullable LiveData value to null [NullSafeMutableLiveData from jetified-lifecycle-livedata-core-ktx-2.4.0-alpha02
iv...@gmail.com <iv...@gmail.com> #17
There is a issue that is still happening on 2.4.0-alpha02 and in the snapshot version mentioned above.
js...@gmail.com <js...@gmail.com> #18
sp...@gmail.com <sp...@gmail.com> #19
I think a new bug report should be created for
ad...@ade.se <ad...@ade.se> #20
Here's a separate issue for the problem in
Description
Component used:
Version used: 2.2.0 and 2.3.0-alpha07
Devices/Android versions reproduced on: N/A
ViewModels with more than one MutableLiveData get their nullability wrong.
Example:
This gives the error
Error: Cannot set non-nullable LiveData value to null [NullSafeMutableLiveData]