Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Read Map Values within Array Firestore Kotlin

Posted on November 11, 2022 By

Solution 1 :

I actually answered that question, but your use case is totally different than the linked one. Why? Simply because “Cars” is an array of maps (custom objects). If you only want to get the value of the “Horsepower” field, then simply use the following lines of code:

val db = Firebase.firestore
val usersRef = db.collection("users")
usersRef.document("TwbJb27eFL3HAS1xLhP1").get().addOnCompleteListener {
    if (it.isSuccessful) {
        val data = it.result.data
        data?.let {
            val cars =  data["Cars"] as List<*>
            for (car in cars) {
                val carMap = car as Map<*, *>
                val horsepower = carMap["Horsepower"]
                Log.d(TAG, "$horsepower")
            }
        }
    }
}

The result in the logcat will be:

120

If you want however to map the “Cars” array into a list of custom objects, then I recommend reading the following resource:

  • How to map an array of objects from Cloud Firestore to a List of objects?

Problem :

I’m trying to retrieve and store the values within this Map in my Firestore. The way I have my Firestore set up is like so:

I’ve found a way to retrieve other fields within my Firestore database but when trying to access map values like so:

horsepowerTextView.text = result.data?.getValue("Horsepower").toString()

it crashes the application and gives me an error saying that “Key Horsepower is missing in the map”.

Can you please tell me how I would be able to get the values within Cars?

The following picture shows Firestore layout and code:

Code within Android Studio

Firestore Layout

I’ve tried this link (how to read mapped data from firestore in kotlin) but when creating the map it only retrieves the topmost variable in the document NOT a part of the Map which would be the values firstName and lastName: Code Tried

Comments

Comment posted by EvanGrant1223

Did exactly as the code showed and it worked! Thank you for the feedback @Alex

READ  [FIXED] android - Is there a way to apply a multiplier to all dimensions within a layout file?
Powered by Inline Related Posts

Android Tags:android, firebase, google-cloud-firestore, google-cloud-platform, kotlin

Post navigation

Previous Post: [FIXED] Unable to connect Firebase in Android Emulator for Flutter Project
Next Post: [FIXED] android – Highlight only one Card in Compose through selection

Related Posts

[FIXED] android – ConstraintLayout include positioning issue Android
[FIXED] android – Koin No definition found for class, but already declared Android
[FIXED] crash – Javascript infinite recursive function crashes Chromium 50 ARM64 lib on Android 10 Android
[FIXED] xamarin – The “XamarinDownloadArchives” task could not be initialized with its input parameters Android
[FIXED] android – add new item of RecyclerView from other fragment Android
[FIXED] flutter – The argument type ‘Future‘ can’t be assigned to the parameter type ‘Future? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme