I am facing a problem to get a return type in a function in kotlin
The function is
override suspend fun uploaded_docs_retrival() : ArrayList<String> {
Firebase.firestore.collection("profiles").whereEqualTo("uid", "$uid").get()
.addOnSuccessListener { result ->
for (document in result) {
val myObject = document.toObject(docretreving::class.java)
val x = myObject.Docs
}
}
return x
}
Here I am retrieving the information from Fire store Mapping it to a object
However in above Function I am unable to return x as it says x is unresolved
My data class is as Follows:
data class docretreving(
val Docs : ArrayList<String> = arrayListOf<String>()
)
How do I write the code in such a way that I can return value x which is a Array list from my function uploaded_docs_retrival()
Edit the below code worked :-
override suspend fun uploaded_docs_retrival() : ArrayList<String> {
return try {
val result = Firebase.firestore.collection("profiles").whereEqualTo("uid", "$uid").get()
.await()
result.first().toObject(docretreving::class.java).Docs
} catch(e: Exception) {
Log.e("TAG", "Failed to upload: ${e.message.orEmpty()}")
ArrayList()
}
}