I want to use LiveData with Kotlin and have values that should not be null. How do you deal with this? Perhaps a wrapper around LiveData? Searching for good patterns here .. As
I little improve answer The Lucky Coder. This implementation cannot accept null values at all.
class NonNullMutableLiveData(initValue: T): MutableLiveData() {
init {
value = initValue
}
override fun getValue(): T {
return super.getValue()!!
}
override fun setValue(value: T) {
super.setValue(value)
}
fun observe(owner: LifecycleOwner, body: (T) -> Unit) {
observe(owner, Observer { t -> body(t!!) })
}
override fun postValue(value: T) {
super.postValue(value)
}
}