1
votes

When setting up Google Authentication using Kotlin, the following error arises: "Type inference failed. Expected type mismatch: inferred type is GoogleSignInAccount? But GoogleSignInAccount was expected". The error is coming from line "val account: GoogleSignInAccount = completedTask.getResult(ApiException::class.java)" This code is coming almost in its entirety from https://developers.google.com/identity/sign-in/android/sign-in

I have checked a question posted on here with the exact same problem but answers to that one couldn't solve my problem (nor her's Type mismatch: inferred type is GoogleSignInAccount? but GoogleSignInAccount was expected > Task :app:buildInfoGeneratorFdroidDebug).

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == RC_SIGN_IN) {
        val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
        handleResult (task)
    }else {
        Toast.makeText(this, "Problem in execution order :(", Toast.LENGTH_LONG).show()
    }
}
private fun handleResult (completedTask: Task<GoogleSignInAccount>) {
    try {
        val account: GoogleSignInAccount = completedTask.getResult(ApiException::class.java) #error in this line
        updateUI (account)
    } catch (e: ApiException) {
        Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show()
    }
}

If anyone has suggestions on what the cause of this error could be, that would be great.

1
just change val account: GoogleSignInAccount to val account: GoogleSignInAccount? - Hayi Nukman
...or remove the explicit type entirely. You don't need type declarations if they can be inferred from assignment. - charles-allen

1 Answers

0
votes

This maybe a late answer, but hopefully it can help the Kotlin beginners like me in the future.

I met exactly the same error, and modify as the following part to fix it:

    val account: GoogleSignInAccount? = completedTask.getResult(ApiException::class.java) 
    updateUI (account!!)

You may refer to Kotlin online document Null Safety for the details about null and "!!" operator.