Nullability and LiveData with Kotlin

前端 未结 8 1591
梦谈多话
梦谈多话 2021-02-19 02:57

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

8条回答
  •  醉酒成梦
    2021-02-19 03:20

    I created an extension property. It's not super pretty, but it is pretty straightforward.

    val  LiveData.valueNN
        get() = this.value!!
    

    Usage

    spinner.loading = myLiveData.valueNN.homeState.loading
    

    I'm not sold on appending "NN" as a good naming convention, but that's beyond the scope of the question :)

提交回复
热议问题