0

Each user in my users collection has three fields: name, age and gender.

I simply want to create a List of all names (the names are Strings). How can I do this?

db.collection("phones").document(applicationID)
    .collection("users")
    .get()
    .addOnSuccessListener { result ->
        // something
        val users: List<String> = result.document.data("name").values // this is obviously the wrong syntax 
}
Zorgan
  • 8,227
  • 23
  • 106
  • 207

1 Answers1

0

You will have to iterate the documents in the QuerySnapshot that your listener receives (your QuerySnapshot variable is result here). That QuerySnapshot contains a DocumentSnapshot for each document in the results. QuerySnapshot implements Iterable, so you can just write a for loop to process each snapshot in order. Or you can use some Kotlin convenience methods:

val names = result!!.map { snapshot ->
    snapshot["name"].toString()
}

names here will be a List<String>.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441