Skip to content Skip to sidebar Skip to footer

Error Retrieving Data From Firebase With Kotlin

I'm trying to retrieve data from firebase, but I get: java.lang.ClassCastException: java.util.HashMap cannot be cast to com.android.mlc.model.Usuario I don't know why the cast fa

Solution 1:

As far as I can see, your UsuariosFirebase.value returns a Map<String, Object>, which indeed can't be cast to Usuario as the error indicates.

To get the data from the DataSnapshot as a Usuario object, you will have to tell Firebase to do that conversion for you:

listaUsuarios.add(UsuariosFirebase.getValue<Usuario>())

Also see the documentation on getting values from Firebase.


The above requires that you have the Kotlin extensions (KTX) for Firebase Realtime Database installed. If you're having a hard time making that work, you can also use this variant, which is a pretty direct rewrite of the Java code:

listaUsuarios.add(UsuariosFirebase.getValue(Usuario::class.java))

Post a Comment for "Error Retrieving Data From Firebase With Kotlin"