I have data wrapper class with parameter T. There is function
in this class with generic container with nullable
type: T? (e.g. LiveData< T?>)
class Wrapper<T> {
fun add (data: LiveData<T?>) {
// ...
}
}
In this place I really need just such type. When I try to use this method and pass non-null type parameter:
class Program {
fun get() {
// some generic container, e.g. LiveData
val data: LiveData<Int> = MutableLiveData()
val wrapper = Wrapper<Int>()
wrapper.add(data)
^^^^ error
}
}
it was 'Type mismatch' error: image link
Type mismatch: inferred type is LiveData<Int> but LiveData<Int?> was expected
How to deal with it? How smart cast generic type parameter from non-null to nullable?
data
you're passing to add function? – noiaverbaledata
contains nullable integers), shouldn't be data declared asval data: LiveData<Int?>
? That would solve the problem immediately. – DVarga