gpt4 book ai didi

java - 如果没有@Provides 注释方法,则无法提供 Dagger 2 LocalDataSource

转载 作者:行者123 更新时间:2023-11-29 19:17:20 25 4
gpt4 key购买 nike

我正在尝试遵循与 here 中相同的示例通过使用 DI 创建存储库模式。问题是我收到以下错误:

"Error:(16, 20) error: @mvp.model.di.scope.Local mvp.model.repository.local.GameLocalDataSource cannot be provided without an @Provides-annotated method. @mvp.model.di.scope.Local mvp.model.repository.local.GameLocalDataSource is injected at mvp.model.repository.GameRepository.(gameLocalDataSource, …) mvp.model.repository.GameRepository is provided at mvp.model.di.component.RepositoryComponent.getGameRepository()"

这是与应用程序相关的代码:

public class GameApplication extends Application {
private RepositoryComponent repositoryComponent;

@Override
public void onCreate() {
super.onCreate();
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
// Normal app init code...
repositoryComponent = DaggerRepositoryComponent.builder()
.applicationModule(new ApplicationModule((getApplicationContext())))
.build();
}

public RepositoryComponent getRepositoryComponent() {
return repositoryComponent;
}
}

这是我的RepositoryComponent:

@Singleton
@Component(modules = {RepositoryModule.class, ApplicationModule.class})
public interface RepositoryComponent {
GameRepository getGameRepository();
}

这是RepositoryModule:

    @Module
public class RepositoryModule {
@Singleton
@Provides
@Local
GameDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}

@Singleton
@Provides
@Remote
GameDataSource provideRemoteDataSource() {
return new GameRemoteDataSource();
}
}

最后,ApplicationModule:

    @Module
public final class ApplicationModule {
private Context context;

public ApplicationModule(Context context) {
this.context = context;
}

@Provides
Context providesContext() {
return context;
}
}

这是我的大部分 GameRepository 类:

@Singleton
public class GameRepository implements GameDataSource {
private GameDataSource remoteDataSource;
private GameDataSource localDataSource;

@Inject
public GameRepository(@Local GameLocalDataSource gameLocalDataSource, @Remote GameRemoteDataSource gameRemoteDataSource) {
remoteDataSource = gameRemoteDataSource;
localDataSource = gameLocalDataSource;
}

此外,正如在提到的示例中,我创建了几个范围,@Local@Remote 因为我的两个数据源具有相同的类型并且 Dagger 需要区分它们。

@Qualifier
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Local {

}

我与 dagger 相关的其余代码,只是构造函数中我想注入(inject)依赖项的 @Inject。此外,DaggerRepositoryComponent 永远不会在 GameApplication 类中生成。

非常感谢您的提前帮助!

最佳答案

GameLocalDataSource cannot be provided without an @Provides-annotated method

在代码的某处你试图@Inject GameLocalDataSource,但你已经在你的模块中指定了如何提供GameDataSource不是 GameLocalDataSource

...
GameDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
...

要么要求Dagger注入(inject)GameDataSource,要么描述Dagger如何提供GameLocalDataSource

...
GameLocalDataSource provideLocalDataSource(Context context) {
return new GameLocalDataSource(context);
}
...

关于java - 如果没有@Provides 注释方法,则无法提供 Dagger 2 LocalDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com