Search code examples
androidkotlinrx-javarx-java2rx-kotlin

How to pass null to an Observable with nullable type in RxJava 2 and Kotlin


I initialize my variable like this:-

 val user: BehaviorSubject<User?> user = BehaviorSubject.create()

But I can't do this. IDE throws an error:-

user.onNext(null)

And doing this, IDE says u will never be null:-

user.filter( u -> u!=null)

Solution

  • Thank you very much for all your answers but I ultimately went with this solution:-

    class UserEnvelope(val user:User?) {}
    

    And using this in the observables.

    This best suited my requirements.

    I am new to Kotlin so I don't know how to use Optionals. But from what I understand, I would have to typecast it to User type everytime I need to observe the values right?