vpbot
vpbot

Reputation: 2487

Type Mismatch required List found LiveData

I'm getting a type mismatch error on the following:

 class LoginViewModel(application: Application) : AndroidViewModel(application) {
     private val authRepository = AuthRepository(database)

     private val _auth = MutableLiveData<List<AuthModel>>()
     val auth: LiveData<List<AuthModel>>
        get() = _auth

     private fun apiLogin(username: String, password: String) {
        viewModelScope.launch {
          try{
              **_auth.value = authRepository.login(username, password)**
            }
        }
    }

class AuthRepository(private val database: xdb) {

       val auth: LiveData<List<AuthModel>> =
        Transformations.map(database.authDao.getAuthInfo()) {
            it.asDomainModel()
        }

      suspend fun login(username: String, password: String): LiveData<List<AuthModel>> {
        withContext(Dispatchers.IO) {
            val auth = Network.retrofitInstance?.create(AuthService::class.java)
            val i = auth?.doLogin(username, password)?.await()
            if (i != null) {
                database.authDao.insertAuthInfo(*i.asDatabaseModel())
            }
        }
        return auth
    }
}


@Dao
interface AuthDao {
    @Query("SELECT * FROM auth")
    fun getAuthInfo(): LiveData<List<AuthEntities>>
}

Why is this code returning a type mismatch _auth.value = authRepository.login(username, password)?

Exact error message:

Type Mismatch
Required List?
Found LiveData<List>

Upvotes: 0

Views: 1545

Answers (1)

Sam Chen
Sam Chen

Reputation: 8847

The data type of _auth.value is List<AuthModel>.

The data type of authRepository.login(username, password) is LiveData<List<AuthModel>>.

They are different.

Try:

_auth.value = authRepository.login(username, password).value

Upvotes: 1

Related Questions