0

this is my function, my problem is: When I'm call this function, first return a null value, after that, the "println" line prints in console all the content of the document and it´s not null

fun readDescriptor(email: String): HashMap<String, Any>? {
    var result: HashMap<String, Any>? = null
    db.collection("users").document(email).collection("descriptor").document("personal").get()
        .addOnSuccessListener { document ->
            if (document.data != null) {
                result = document.data as HashMap<String, Any>
                println("Content of document: $document.data")
            } else {
                result = null
            }
        }
    return result
}

how can I get objectively if a document exist or not? ty

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Please only use the `android-studio` or `intellij` tag for questions about the IDE itself. For questions about Android programming in general, use the `android` tag. – Frank van Puffelen May 16 '22 at 00:13
  • See also [this](https://stackoverflow.com/a/70178210/9473786) - it is an async call, your return value will always be null even if it does exist. You cannot return the value set in the listener. The code in your success listener runs later in the future after this method already returned. – Tyler V May 16 '22 at 00:26

0 Answers0