Skip to content Skip to sidebar Skip to footer

Dagger2 Allow Injection To Be Null

So, in short, I have a lot of flavors of the same app with slight differences. I would like to add a Navigator for all flavors independently. I'm using Dagger2, and I have a sub co

Solution 1:

To support a nullable type you just have to add a @Nullable annotation on your @Provides method that returns the nullable type and Dagger will accept it.

If you don't, Dagger will complain that you can only provide nullable types from @Nullable annotated methods.

Solution 2:

if you don't want to go down the road of having nullable objects injected, you can return an Optional<Navigator> in your dagger module - you'll end up with cleaner code in the long run.

Post a Comment for "Dagger2 Allow Injection To Be Null"