Search code examples
javaandroidkotlindependency-injectiondagger

Dagger Issue - Generate useCase has error


i created my useCase and setup input and output and then provide it on module and everything works fine.


class GetProfileUseCase constructor(
    private val repository: Repository,
    defaultDispatcher: CoroutineDispatcher
) : SuspendUseCase<Request, ResultEntity<Response>>(defaultDispatcher) {

    override suspend fun execute(parameters: Request): ResultEntity<Response> {
        return repository.getProfile(parameters)
    }
}

and here is my data class


sealed class GetProfile : Serializable {

    data class Request(
        val nothing: Nothing? = null
    ) : GetProfile()

    data class Response(
        val user: User? = User()
    ) : GetProfile()
}

because there is no parameter in Request class i tried changed it to :

object Request

but when build it IDE error :

error: method create in class ViewModel_Factory cannot be applied to given types;

and here is my view model :

class ViewModel @Inject constructor(
    private val getProfileUseCase: GetProfileUseCase
) : BaseViewModel()

i don't know why it happen and dagger couldn't generate classes !!!

it works without no error when set Request as data class but crash when make it object class


Solution

  • because there is no parameter in Request class i tried changed it to : object Request

    You seem to think an object is same as a class with a single no parameter constructor, which is not true. For complete understanding I encourage you to read this thread. However I will try to provide a brief explanation as to why your code doesn't work

    When you do object Request, it makes Request a singleton, meaning you can no longer create its instances using val instance = Request(), reason being that constructor of Request is marked private by kotlin compiler.

    so when your code(dagger) tries to inject Request using new Request(), it gives you error.