Ale
09/07/2020, 7:58 PMprivate fun extractLevels(querySnapshot: QuerySnapshot): List<Level?> {
return querySnapshot.documents
.map { it.toObject(Level::class.java) }
}
If the list of documents is empty, this should just return an empty list, right? There shouldn't be nulls in the list. However, unless I set the return type as List<Level?>), I get an error because it's actually expecting that. What am I doing wrong, and how can I have this return a List<Level> ?Tobias Berger
09/07/2020, 8:10 PMtoObject
returns something nullable, so the resulting map could (from a compiler POV) contain null valuesTobias Berger
09/07/2020, 8:13 PM!!
after the toObject
call, but I personally find this to be a bad practice.
If nulls aren't an issue here, you can just replace map
witch mapNotNull
. That will check each mapping result and just exclude nulls from the resulting list, which then also has the non-null generic type.Ale
09/07/2020, 8:20 PM