As the `Observer` interface for `LiveData` only ha...
# android
t
As the
Observer
interface for
LiveData
only has one
observe
function, why should we write
Observer { ... }
and not directly a lambda ? Also, wouldn't it be nice if the parameter of the
Observer
lambda would take the nullability of the
T
value from LiveData into account ?
Copy code
val nonNullable: LiveData<String> = ...
nonNullable.observe(this) { value: String -> ... }
val nullable: LiveData<String?> = ...
nullable.observe(this) { value: String? -> ... }
m
You can use
androidx.lifecycle:lifecycle-livedata-ktx
and
import androidx.lifecycle.observe
function that lets you write lambda directly as
someLiveData.observe(viewLifecycleOwner).observe {}