I am making an android app with kotlin, but I am currently facing a strange issue. The function where the issue is this:
private fun getName(uids:List<String>):List<String> {
val nameList = mutableListOf<String>()
for (uid in uids) {
Firebase.firestore.collection("users").whereEqualTo("id", uid).get().addOnSuccessListener { documents ->
val name = documents.documents[0]["name"] as String
println("The name is $name")
nameList.add(name)
println("The namelist is $nameList")
}
println("The data above is $nameList")
}
println("The datahere is $nameList")
return nameList.toList()
}
Here I have a few ids of the user and based on these ids I am fetching the name from the Firebase Cloud Firestore database, the data is successfully being fetched and when I print the namelist first time inside the fetching block, an element is being added. However, when I print the nameList outside the fetching block, I see that the element does not add. I have attached an image.

As you can see the element is being added. But after that, the element disappears and the list becomes empty.

I am really confused as to why this is occurring.