How to return a list from Firestore database as a result of a function in Kotlin?

前端 未结 3 1552
死守一世寂寞
死守一世寂寞 2020-11-22 01:56

I\'m building an app for a friend and I use Firestore. What I want is to display a list of favorite places but for some reason, the list is always empty.

I

3条回答
  •  你的背包
    2020-11-22 02:43

    The reason for having a empty list got perfectly answered by Alex Mamo above.

    I just like to present the same thing without needing to add an extra interface.

    In Kotlin you could just implement it like so:

    fun readData(myCallback: (List) -> Unit) {
        placesRef.get().addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val list = ArrayList()
                for (document in task.result) {
                    val name = document.data["name"].toString()
                    list.add(name)
                }
                myCallback(list)
            }
        }
    }
    

    and then use it like so:

    readData() {
       Log.d("TAG", it.size.toString())
    })
    

提交回复
热议问题