How to wait until if statement is done? Kotlin

Viewed 33

I try to return an array of cards. The list with card is empty at the moment when I return the list. But that's only because the if statement is not finished. Does someone know how I can wait until the if statement is done?

fun getAllCards(): MutableList<Card> {
    val cards = mutableListOf<Card>()

    fireStore.collection("cards")
        .get()
        .addOnCompleteListener {
                task ->
            if (task.isSuccessful) {

                for (document in task.result) {
                    cards.add(Card((document.data["value"]).toString().toFloat()))
                }

            } else {
                Log.w(TAG, "Error getting documents.", task.exception)

            }

        }
   return cards
}
0 Answers
Related