https://kotlinlang.org logo
#android
Title
# android
o

Ofir Bar

03/12/2020, 8:53 AM
Source code:
Copy code
fun getCityById(selectedCityId: Int): City {
    var city : City? = null
    viewModelScope.launch {
        city = locationRepository.getSingleCityFromDatabaseById(selectedCityId)
    }
    return city //Compilation error message: type mismatch, Required:City, found: City?
}

fun getListOfLanguagesByIds(selectedLanguages: List<Int>): List<Language> {
    viewModelScope.launch {
        return supportedLanguagesRepository.getLanguagesFromDatabaseById(selectedLanguages.toIntArray())
    }// Compilation error above message: 'return' is not allowed here
}

fun getExpertiseFieldById(expertiseFieldId: Int): ExpertiseField {
    lateinit var expertiseField : ExpertiseField
    viewModelScope.launch {
        expertiseField = expertiseFieldsRepository.getSingleExpertiseFieldFromDatabaseById(expertiseFieldId)
    }
    return expertiseField
    //Seems to work, but I am afraid expertiseField will be returned uninitialized.
}
r

Raul Tunduc

03/13/2020, 7:11 AM
why you do not use LiveData?
3 Views