Dagger2 non @Nullable @Provides method

I am at the beginning of using Dagger for my projects. I created the following modules:

@Module
public class FirebaseModule {

@Provides @Singleton
public FirebaseUser provideCurrentUser(){
    return FirebaseAuth.getInstance().getCurrentUser();
}

@Provides @Singleton
public DatabaseReference provideDatabaseReference(){
    return FirebaseDatabase.getInstance().getReference();
}

@Provides @Singleton
public FirebaseAuth provideFirebaseAuth(){
    return FirebaseAuth.getInstance();
}
}

      

and this:

@Module
public class AppModule
{
private HappyParkApp app;

public AppModule(HappyParkApp app) {
    this.app = app;
}

@Provides
public HappyParkApp providesApp()
{
return this.app;
}

@Provides
public Context getAppContext(){
    return this.app;
}
}

      

I also created a component:

@Singleton
@Component(modules = {AppModule.class,
                      FirebaseModule.class})
public interface AppComponent {

    void inject(MainActivity activity);
}

      

and this is the app implemented in the app:

public class HappyParkApp extends Application {

private AppComponent appComponent;
private static HappyParkApp instance = new HappyParkApp();
@Override
public void onCreate() {
    super.onCreate();
    createAppComponent();
}

public static HappyParkApp getInstance() {
    return instance;
}

public AppComponent createAppComponent() {
 if(appComponent == null){
     appComponent = DaggerAppComponent.builder()
             .appModule(new AppModule(this))
             .firebaseModule(new FirebaseModule()).build();
 }

 return appComponent;
}

      

}

so I tried to do this in the onCreate()

MainActivity method:

HappyParkApp.getInstance().createAppComponent().inject(this);

      

and this before:

 @Inject
FirebaseUser user;

@Inject
DatabaseReference reference;

@Inject
FirebaseAuth mAuth;

      

but I am getting this error:

 Caused by: java.lang.NullPointerException: Cannot return null from a non-@Nullable @Provides method

      

on this line:

 HappyParkApp.getInstance().createAppComponent().inject(this);

      

and I don't know how to fix it: what is the error? Is this the wrong injection?

thank

+3


source to share


2 answers


Cannot return null from @@ Nrovable @Provides method

You are trying to provide null

a provider method that is not marked as @Nullable

.

Have a look at getCurrentUser ()

Returns the currently connected FirebaseUser, or null if not present.

Thus, if the user does not have a subscribed user, it will null

, making the following invalid ...

@Provides @Singleton
public FirebaseUser provideCurrentUser(){
  return FirebaseAuth.getInstance().getCurrentUser(); // could be null!
}

      



So, to fix this, you have 2 options:

  • provide FirebaseAuth

    and capture the user in your application code, or
  • mark the supplier as @Nullable

Which approach is best depends on your setup, but since you are placing the user in a scoped component @Singleton

, I recommend not exposing the user as it can change during the lifetime of the Singleton.

Move it to a different area (like @UserScope

or @PerUser

) or just take the user where you need it using FirebaseAuth.getCurrentUser()

. You can still provide FirebaseAuth

.

Also reads about invalidity here .

+9


source


if you annotate your provider method with @Nullable and forget to use @Nullable when you want to use that object, dagger will throw an error ( is not nullable but is being provided by @Provides @android.support.annotation.Nullable

)

so just use if you provide a nullable object be sure to add @Nullable for every method that uses that object as a parameter



ex:

@Nullable
@Provides
static Object provideHomePost(MainActivity activity){
    return activity.getObject();
}
@Provides
static Presenter providePresenterFactory(@Nullable HomePost post) {
    return new CommentPresenterImpl(post);
}

      

0


source







All Articles