Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

617
Visualizações
Return string from Firebase addOnSuccessListener

I am programming the backend of a multiplayer game for a school project. I need to load a random document (more specifically a user's username) from a collection of users. That part works. Basically, I run into an issue where the username stays null ("") when I call the function, instead of the username that should be loaded with the docRef.get() function. Below is the code that I wrote, however I've found that it doesn't only happen with this function, but with all the ones that use the docRef.get() function.

Any help would be much appreciated, as I've been stuck on this issue for over a week. It's my first project in Kotlin/Firebase.

Thanks.

fun getRandomPlayerUsername() : String{

     var db = FirebaseFirestore.getInstance() // Loads Firebase functions

     var loadedPlayer:LoadedPlayer? = null

     val random = (0 until 3).random()

     val docRef = db.collection("users").orderBy("username").limit(3)

     var username:String = ""



     docRef.get().addOnSuccessListener { documentSnapshot ->

          val playerList: MutableList<out LoadedPlayer> = documentSnapshot.toObjects(LoadedPlayer()::class.java)

          loadedPlayer = playerList[random]

          username = loadedPlayer?.username.toString()
     }

     return username
}
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda