如何修复 "cannot be provided without an @Provides-annotated method." - Kotlin

how to fix "cannot be provided without an @Provides-annotated method." - Kotlin

每当我尝试 运行 我遇到这个错误的应用程序:

error: [Dagger/MissingBinding] api.PrayerTimesInterface cannot be provided without an @Provides-annotated method.

这是界面:

    @GET("calendar")
suspend fun getPrayerTimes(
    @Query("latitude") latitude: Double,
    @Query("longitude") longitude: Double,
    @Query("method") method: Int,
    @Query("month") month: Int,
    @Query("year") year: Int,
): Response<PlayerTime>

@GET("calendarByAddress")
suspend fun getPrayerAddress(
    @Query("address") address: String,
    @Query("method") method: Int,
    @Query("month") month: Int,
    @Query("year") year: Int,
): Response<PlayerTime>
}

我尝试添加“@Provides”,但仍然面临同样的错误。 相同的代码在不同版本的应用程序中可用,并且工作正常。

我在这里尝试了解决方案: How to fix "cannot be provided without an @Provides-annotated method" error in dagger2 library

但它对我不起作用

我是不是做错了什么?

Interface classes 不能直接注入,因为它们没有构造函数。我想您正在使用 PrayerTimesInterfaceRetrofit。所以用Retrofitcreate方法注入PrayerTimesInterface

moduleclass

@Provides
internal fun providesPrayerTimes(): PrayerTimesInterface {
        return Retrofit.Builder().build().create(PrayerTimesInterface::class.java)
    }

您还可以使用所需的所有选项创建 Retrofit 注入,并将其放入 providesPrayerTimes 方法中以创建 PrayerTimesInterface