I'm retrieving a list of documentSnapshot from Fir...
# announcements
a
I'm retrieving a list of documentSnapshot from Firestore, coverted to POJOs:
Copy code
private 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> ?
t
probably
toObject
returns something nullable, so the resulting map could (from a compiler POV) contain null values
if you want to handle that case, you can do so in the map-lambda. If you are sure that null is impossible, you could do
!!
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.
a
ah, brilliant, thank you!