When creating a LiveData in kotlin is there really...
# android
m
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
Not sure LiveData can handle nullability
m
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
Might be a reminder yes
a
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.