1

I am trying to read mapped data from Firestore. I am reading data but I do not know how to reach one of the keys in the map.

this is mapped data

dbSaveAvailableTimes.addSnapshotListener { snapshot, e -> if (snapshot != null && snapshot.exists()) { val chaplainAvailableTimes = snapshot.data if (chaplainAvailableTimes != null) { Log.d("chaplainAvailableTimes", chaplainAvailableTimes.toString()) } 

I read the data here.

D/chaplainAvailableTimes: {1637868400000={patientUid=null, isBooked=false, time=1637868400000}, 1637863200000={patientUid=null, isBooked=false, time=1637863200000}}

I get results like this in the log.

when I try the chaplainAvailableTimes["time"], I get null.

So, how can I take the time key data after that? I am not familiar with the map in Kotlin. thanks for your help in advance?

4
  • Please edit your question and add your database structure as a screenshot. Commented Nov 23, 2021 at 7:52
  • hi, I added the screenshot. if you click "this is mapped data", you will see the database structure. thanks Commented Nov 23, 2021 at 8:02
  • So to understand better, you want to read the value of "time" that exists under each object, right? Commented Nov 23, 2021 at 8:29
  • @kadiryapar Do you want a list of these times as there are many of them OR just the first one? Commented Nov 23, 2021 at 9:06

3 Answers 3

1

According to my last question:

So to understand better, you want to read the value of "time" that exists under each object, right?

And your last answer:

hi, yes. thanks

To get the values of "time" from within each object, please use the following lines of code:

val db = FirebaseFirestore.getInstance() val availableTimesRef = db.collection("chaplainTimes").document("availableTimes") availableTimesRef.get().addOnCompleteListener { if (it.isSuccessful) { val data = it.result.data data?.let { for ((key, value) in data) { val v = value as Map<*, *> val time = v["time"] Log.d(TAG, "$key -> $time") } } } } 

The result in the logcat will be:

1637863200000 -> 1637863200000 1637868400000 -> 1637868400000 
Sign up to request clarification or add additional context in comments.

Comments

1

You can get the list of times using this code:

// This will be a List<String> val timeList = snapshot.data.values.map { (it as Map<*, *>)["time"] as String } 
  • snapshot.data returns the MutableMap<String,Any>
  • .values returns the collection of map values Collection<Any>
  • .map transforms each value to get a List<String>
  • inside map we cast each value to a generic map Map<*,*>, fetch the value for key time and cast it to a String

Comments

0

Try to cast chaplainAvailableTimes as val theData = chaplainAvailableTimes as? HashMap<*, *>. After that you will be able to iterate over it or get the data like this:

theData.values.first().get("time") // returns 1637868400000 

7 Comments

hello, I tried it but it still returns null. Also, there is no option .first[ ]. What do you mean with that. thanks
Try to force cast it (as, without ?) and post the error message here. If cast is successful, the result should be map. Map has .values.first property which returns first element (same as values.[0]). I've updated the code for map (was for array before, sorry).
hello, 'theData.values.first()' method is available but 'theData.values.first["time"]' this one is not available. 'theData.values.first()' this method return the first element of the array which is {patientUid=null, isBooked=false, time=1637868400000}. I want to reach time key under every object. thanks for your support.
I've updated the code to get time. Check the main answer.
hi, if you mean the update "theData.values.first()["time"]" this code, it is not working. after the first() method, it does not accept ["time"] .
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.