https://kotlinlang.org logo
#android
Title
# android
m

Michael Friend

02/24/2020, 3:01 PM
When creating a LiveData in kotlin is there really any difference between
LiveData<String>
and
LiveData<String?>
? Since all the methods in LiveData are nullable the the types in
observe()
are both nullable and it doesn’t prevent you from setting either value as null. Is it just as a reminder not to set it null?
s

satyan

02/24/2020, 4:48 PM
Not sure LiveData can handle nullability
m

Michael Friend

02/24/2020, 4:49 PM
It doesn’t as far as I can tell. Just noticed that this code lab uses a nullable type for one LiveData and a non nullable type for another https://codelabs.developers.google.com/codelabs/advanced-kotlin-coroutines/?authuser=0#13
s

satyan

02/24/2020, 4:49 PM
Might be a reminder yes
a

Andrew

02/24/2020, 5:00 PM
You can recreate a nonnull wrapper for livedata which is passed a default value. This works well for simple cases where you are just changing it via myLiveData.value = whatever. Gets more complicated when you start using the maps or livedata from a repository where its loading something async. In those cases even though you'd like your data to never be null the short time between loading and loaded is a weird state, you know its never going to be null... eventually, so you gotta handle that case by allowing null.