Issue
I'm trying to retrieve data from Firebase so I'm using Hashmap for it, but I wanna know why casting Hashmap, why can't use it directly. Please give a deep and easy language answer (should be in Kotlin)
Here is the code:
var map = snapshot.getValue() as Hashmap<String,String>
Solution
When called on a DataSnapshot
that contains multiple values, getValue()
returns a Map<String,Object>
since (while the keys are by definition strings) it can't know what types all of the values in your database are.
If you know all the values are strings, you can cast the result to a Map<String,String>
as you do, but it does require you to cast the result you get back from getValue()
.
Answered By - Frank van Puffelen