I have a function that retrieves data from Firebase. When it retrieved data ı store that data with for loop in a string variable. But when i tried to use that string variable that function returns an empty string. I know that for loop is working async but ı can 't handle the storing data in string variable async too. Appreciated for any help.
fun getUser() : String{
var username: String =""
val docRef = database.collection("Users").whereEqualTo("email", currentUserEmail!!)
docRef.get().addOnSuccessListener {
val documents= it.documents
for (document in documents){
username=document.get("userName") as String
}
}
return username
}
}