How do you people deal with LiveData nullability? ...
# android
e
How do you people deal with LiveData nullability? I used it for data binding but it never nulls
m
I made this little extension method that I use everywhere:
Copy code
inline fun <T> LiveData<T>.observeNotNull(
    lifecycleOwner: LifecycleOwner,
    crossinline method: Function1<T, Unit>
) {
    observe(lifecycleOwner, Observer {
        if (it != null) {
            method(it)
        }
    })
}
and then I always call
.observeNotNull
not
observe
e
Thanks! What about when you call
value
?
m
I never do that.
👍 1
But I guess you can do
val value = liveData.value ?: error ("X should be non-null")
if you are sure it cannot be null