Issues maving data around function


Hello, I want to mave data from an inner function to the main function and i dont know how

fun getData():ArrayList {
val posts= ArrayList()
val ref= FirebaseDatabase.getInstance().getReference(“post”)
ref.addListenerForSingleValueEvent(object : ValueEventListener {
override public fun onDataChange(snapshot: DataSnapshot?){
val ps:ArrayList = ArrayList()
val children = snapshot!!.children
children.forEach {
val post=it.getValue(
posts.forEach {

        override public fun onCancelled(p0: DatabaseError?) {
            Log.v("dfb","An error dbfirebase")

    return posts

Its getting the data from the database but the function returns nothing