N
N
Nadir Parilaev2021-08-02 12:58:48
Kotlin
Nadir Parilaev, 2021-08-02 12:58:48

How to get a variable from a function?

How to get data from the res1 variable outside the function so that it can be used later in the code.

fun lastCarpetId( )  {
        
        val db: FirebaseFirestore = FirebaseFirestore.getInstance()
        val res1 = StringBuffer()
        db.collection("carpets")

            .orderBy("carpetid")
            .limit(1)
            .get()
            .addOnCompleteListener {


                if (it.isSuccessful) {
                    for (document in it.result!!) {
                        res1.append(document.data.getValue("carpetid")).append("")


                    }
                }

            }

    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-08-04
@Joker4567

lastCarpetId { value ->
            //value получен
        }

fun lastCarpetId(callback: (String) -> Unit)  {
        val db: FirebaseFirestore = FirebaseFirestore.getInstance()
        db.collection("carpets")
            .orderBy("carpetid")
            .limit(1)
            .get()
            .addOnCompleteListener {
                if (it.isSuccessful) {
                    for (document in it.result!!) {
                        callback(document.data.getValue("carpetid"))
                    }
                }
            }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question