dagger2 非@Nullable @Provides 方法

dagger2 non-@Nullable @Provides method

我刚开始在我的项目中使用 Dagger。 我制作了这个模块:

@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();
}
}

还有这个:

@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;
}
}

我也做了组件:

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

    void inject(MainActivity activity);
}

这是应用程序中实现的 appComponent:

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;
}

}

所以我尝试在 MainActivity 的 onCreate() 方法中做到这一点:

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

之前这个:

 @Inject
FirebaseUser user;

@Inject
DatabaseReference reference;

@Inject
FirebaseAuth mAuth;

但我收到此错误:

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

在这一行:

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

我不知道如何解决:错误是什么?这是注射错误吗?

谢谢

Cannot return null from a non-@Nullable @Provides method

您尝试通过 而非 标记为 @Nullable.

的提供程序方法提供 null

看看getCurrentUser()

Returns the currently signed-in FirebaseUser or null if there is none.

所以如果没有登录用户,这将是 null,使以下无效...

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

所以要修复它,您有 2 个选择:

  • 提供 FirebaseAuth 并在您的应用程序代码中获取用户,或者
  • 将提供商标记为 @Nullable

哪种方法更好取决于您的设置,但由于您将用户放在 @Singleton 范围内的组件中,我建议 不要 提供用户,因为它很可能在“单身人士”的生命周期内改变。

要么将其移动到不同的范围(例如 @UserScope@PerUser),要么使用 FirebaseAuth.getCurrentUser() 在需要的地方抓住用户。您仍然可以提供 FirebaseAuth.

还有read about nullability here.

如果你用@Nullable 注释你的提供者方法,而忘记在你想使用该对象时使用@Nullable,匕首将抛出错误(is not nullable but is being provided by @Provides @android.support.annotation.Nullable)

因此,如果您提供可为空的对象,请确保将@Nullable 添加到使用该对象作为参数的每个方法中

例如:

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