I'm using methods (for example the method containsKey of MutableList) that returns a true false. Since the function is written in java, Kotlin refers to return type as Boolean? and this the reason i'm getting a compilation error: "Required: Boolean, Found: Boolean?". I must say in some case (don't know why) using the method is ok and sometimes it return the error above. Can someone guess what is the reason?
My code:
val gamesPerCountriesMap = mutableMapOf<String,MutableMap<Long, List<AllScoresGameObj>>>()
if (countryName != null && countryName != "" && !gamesPerCountriesMap.containsKey(countryName))
{
gamesPerCountriesMap.put(countryName, mutableMapOf<Long,List<AllScoresGameObj>>())
}
if (!gamesPerCountriesMap.get(countryName)?.containsKey(competitionId))
{
gamesPerCountriesMap.get(countryName)?.put(competitionId, listOf<AllScoresGameObj>())
}
First if is compiled the second one make the error:
Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Boolean?
if I remove the ! in the beginning of the second if i will different error:
Type mismatch: inferred type is Boolean? but Boolean was expected
After reading all the suggestion i wrote the following code:
gamesPerCountriesMap.get(countryName)?.let {
if (!gamesPerCountriesMap.containsKey(it))
{
gamesPerCountriesMap.get(countryName)?.put(competitionId, listOf<AllScoresGameObj>())
}
}
What do you think?