Retieve a HashMap from Firebase using Kotlin

I'm trying to retrieve a list of object "Game" from my firebase database using Kotlin on Android Studio.

my firebase database

I am using the code below:

private fun getGamesAvailable() {
        var ref = FirebaseDatabase.getInstance().getReference("games")
        var gamesMutableList: MutableList<Game> = mutableListOf()
        val menuListener = object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (dataValues in dataSnapshot.children) {
                    val game: Game? = dataValues.getValue(Game::class.java)
                    gamesMutableList.add(game!!)
                }
                val adapter = GameAdapter(applicationContext, R.layout.games_list, gamesMutableList)
                listViewGames.adapter = adapter
            }
            override fun onCancelled(databaseError: DatabaseError) {
                // handle error
            }
        }
        ref.addListenerForSingleValueEvent(menuListener)
}

but I get the error

com.google.firebase.database.DatabaseException: Expected a List while deserializing, but got a class java.util.HashMap

I understand that the problem is I am trying to retrieve a list of Games that has a list of Players, but I don't know how to solve this. I found some posts about it on stackoverflow but I don't really understand the answers and how they solve this problem.

Android-Expected a List while deserializing, but got a class java.util.HashMap

Expected a List while deserializing, but got a class java.util.HashMap

If anyone could help me understand I'll be very thankfull !

2 answers

  • answered 2020-02-13 00:10 Frank van Puffelen

    It's hard to be certain without seeing Game. But my guess it that you've modeled mplayers as a List<Player> or Player[]. Where Player would be the class that models the data for each player under mplayers.

    Firebase has a very strict definition of what an array/list looks like, and player names as the keys does not meet that definition.

    What you're showing for mplayers in the JSON would translate to Map<String, Player> in your code.

  • answered 2020-02-13 00:12 malta59

    i hope you have not solved this issue by the moment i post my "answer", wink wink.

    I assume you get the error on this line

    val game: Game? = dataValues.getValue(Game::class.java)
    

    What always helps me in this cases is to place a breakpoint where i get the data from the service the use the Evaluate Expression (Alt+F8) to see the hidden claseess the object gives back, and undestand better what is inside the box.

    You may not be able to parse it directly to your Game class at the moment but by looking inside the object you may be able to get the portions of the info that you need ,then you may find easier to parse that portion of the data.

    I have not used DataSnapshots , yet but

    Let me give you an example with a DocumentSnapshot

    Evaluate Expression

    Then you can search what i has inside and parse the data one by one or by other methods you may now.

    fields

    That usually helps me when i don't find an easy answer online or i don't have the necesary models to parse the data.

    I hope i have been of help Guillaume, Have a nice day, and a proper solution to you issue.