Home > Enterprise >  How can I use Hash map from firebase database in android
How can I use Hash map from firebase database in android

Time:09-22

val usersref = RTDBref.child("users")
    
    usersref.get().addOnSuccessListener { datasnapshot ->
        val datas = datasnapshot.value
        Log.i("datas", datas.toString()) //it returns hash map

        val id = datas.get("Id") //can't use , redline here
    }

datasnapshot.value returns {Id=userId, uid=uid}. It's a hash map.

So I wanna use val id = datas.get("Id") to get data from a hash map,

But I can't use it because val datas is Type of Any? before retrive data from firebase database.

Is there any way to use get() method to val datas please?..

CodePudding user response:

Use a cast.

(datas as? Map)?.get("Id") 

This will tell the compiler to treat it as a map. If data is not a map, it will set datasMap to null, and the ?. operator will make the entire statement return null in that case.

  • Related