Skip to content

Instantly share code, notes, and snippets.

@RBusarow
Created December 28, 2019 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RBusarow/85200b47cd50258c86b77779c1a910a1 to your computer and use it in GitHub Desktop.
Save RBusarow/85200b47cd50258c86b77779c1a910a1 to your computer and use it in GitHub Desktop.
nullability-safe Kotlin version of MutableLiveData
@Suppress("UNCHECKED_CAST")
class MutableLiveData2<T>(value: T) : LiveData<T>(value) {
override fun getValue(): T = super.getValue() as T
public override fun setValue(value: T) = super.setValue(value)
public override fun postValue(value: T) = super.postValue(value)
}
@way2jatin
Copy link

This seems to be a great alternative to avoid null checks while retrieving values in mutable live data. But i m getting an issue where this can't be used in two-way data binding. I get the following error

The expression 'viewModelValue.getValue()' cannot be inverted, so it cannot be used in a two-way binding

Details: There is no inverse for method getValue, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions

If possible, could you please have a look on what can be done here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment