没有 @provides-annotated 方法就无法提供 - Dagger/Hilt
Cannot be provided without an @provides-annotated method - Dagger/Hilt
我正在尝试将 Dagger Hilt 与 Retrofit 和 Corroutines 结合使用来制作一个简单的 API 消费项目。但是,每当我尝试 运行 应用程序时,“如果没有 @Provides-annotated 方法,就无法提供 DataQuoteApi。”老实说,我不知道如何解决这类问题,因为我不能在界面中添加 @Provides 或类似的东西
我的界面:
interface DataQuoteApi {
@GET("/quotes")
suspend fun getAllQuotes(): List<QuoteModel>
}
我的存储库:
class QuoteRepository @Inject constructor(
private var api:DataQuoteApi
) {
suspend fun getQuotes():List<QuoteModel>?{
return api.getAllQuotes()
}
}
我的模块:
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideRetrofit(): Retrofit {
return Retrofit
.Builder()
.baseUrl("https://thesimpsonsquoteapi.glitch.me")
.addConverterFactory(GsonConverterFactory.create()).build()
}
@Singleton
@Provides
fun provideGetQuotes(apiQuote : DataQuoteApi): QuoteRepository {
return QuoteRepository(apiQuote)
}
}
梅乌视图模型:
@HiltViewModel
class QuoteViewModel @Inject constructor(
private var repository: QuoteRepository
):ViewModel() {
var isLoading = MutableLiveData<Boolean>()
var quote = MutableLiveData<QuoteModel>()
fun getQuotes(){
viewModelScope.launch {
isLoading.postValue(true)
var response = repository.getQuotes()
if(!response.isNullOrEmpty()){
quote.postValue(response[0])
isLoading.postValue(false)
}
}
}
fun onCreate() {
viewModelScope.launch {
isLoading.postValue(true)
var allQuotes = repository.getQuotes()
if (!allQuotes.isNullOrEmpty()) {
quote.postValue(allQuotes[0])
isLoading.postValue(false)
}
}
}
}
有人知道我做错了什么吗?
让我告诉你一个关于 Dagger 实际工作原理的简短故事。
Dagger 是一个完全静态和编译时间的依赖注入库。
With Annotation @Inject in a class constructor or field or method(实际上已弃用,不要使用它),Dagger 将此 class 或 Object 添加到其依赖关系图中.
Dagger 知道如何实例化一个没有依赖关系的简单 class,就像这个例子:
class Example @Inject constructor() {}
这是一个 class 和 no-args 构造函数,dagger 可以实例化这个 class 并将其添加到依赖关系图中。
在某些情况下喜欢使用 third-party 库,这些库使用构建器模式或接口和抽象 class 无法轻松实例化 dagger 不知道如何创建其实例 class 或依赖项,因此您必须使用 Module 并告诉他们如何创建该依赖项的实例。
照这个例子做:
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideRetrofit(): Retrofit {
return Retrofit
.Builder()
.baseUrl("https://thesimpsonsquoteapi.glitch.me")
.addConverterFactory(GsonConverterFactory.create()).build()
}
// You provide retrofit so dagger use it in this method
@Singleton
@Provides
fun provideGetQuotes(retrofit: Retrofit): DataQuoteApi {
return retrofit.create(DataQuoteApi::class.java)
}
}
我正在尝试将 Dagger Hilt 与 Retrofit 和 Corroutines 结合使用来制作一个简单的 API 消费项目。但是,每当我尝试 运行 应用程序时,“如果没有 @Provides-annotated 方法,就无法提供 DataQuoteApi。”老实说,我不知道如何解决这类问题,因为我不能在界面中添加 @Provides 或类似的东西
我的界面:
interface DataQuoteApi {
@GET("/quotes")
suspend fun getAllQuotes(): List<QuoteModel>
}
我的存储库:
class QuoteRepository @Inject constructor(
private var api:DataQuoteApi
) {
suspend fun getQuotes():List<QuoteModel>?{
return api.getAllQuotes()
}
}
我的模块:
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideRetrofit(): Retrofit {
return Retrofit
.Builder()
.baseUrl("https://thesimpsonsquoteapi.glitch.me")
.addConverterFactory(GsonConverterFactory.create()).build()
}
@Singleton
@Provides
fun provideGetQuotes(apiQuote : DataQuoteApi): QuoteRepository {
return QuoteRepository(apiQuote)
}
}
梅乌视图模型:
@HiltViewModel
class QuoteViewModel @Inject constructor(
private var repository: QuoteRepository
):ViewModel() {
var isLoading = MutableLiveData<Boolean>()
var quote = MutableLiveData<QuoteModel>()
fun getQuotes(){
viewModelScope.launch {
isLoading.postValue(true)
var response = repository.getQuotes()
if(!response.isNullOrEmpty()){
quote.postValue(response[0])
isLoading.postValue(false)
}
}
}
fun onCreate() {
viewModelScope.launch {
isLoading.postValue(true)
var allQuotes = repository.getQuotes()
if (!allQuotes.isNullOrEmpty()) {
quote.postValue(allQuotes[0])
isLoading.postValue(false)
}
}
}
}
有人知道我做错了什么吗?
让我告诉你一个关于 Dagger 实际工作原理的简短故事。
Dagger 是一个完全静态和编译时间的依赖注入库。
With Annotation @Inject in a class constructor or field or method(实际上已弃用,不要使用它),Dagger 将此 class 或 Object 添加到其依赖关系图中.
Dagger 知道如何实例化一个没有依赖关系的简单 class,就像这个例子:
class Example @Inject constructor() {}
这是一个 class 和 no-args 构造函数,dagger 可以实例化这个 class 并将其添加到依赖关系图中。
在某些情况下喜欢使用 third-party 库,这些库使用构建器模式或接口和抽象 class 无法轻松实例化 dagger 不知道如何创建其实例 class 或依赖项,因此您必须使用 Module 并告诉他们如何创建该依赖项的实例。
照这个例子做:
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideRetrofit(): Retrofit {
return Retrofit
.Builder()
.baseUrl("https://thesimpsonsquoteapi.glitch.me")
.addConverterFactory(GsonConverterFactory.create()).build()
}
// You provide retrofit so dagger use it in this method
@Singleton
@Provides
fun provideGetQuotes(retrofit: Retrofit): DataQuoteApi {
return retrofit.create(DataQuoteApi::class.java)
}
}