what i am trying to do is fetch data from firebase and then display it on screen as a text using jetpack composer
@Composable
fun showList(user:String){
db.collection(myCollection).document(user).get().addOnSuccessListener {
if(it.exist()){
val datos = it.get("list_data")
val listDatos = listOf(datos)
for(data in listDatos){
Text(text=data) //funcion composable
}
}
}
however, it gives me the error @Composable invocations with only happen from thhe context of an @Composable function and I don't know why
Composable functions are like kotlin suspend functions , in that they can only be called from a specific context. By writing inside
addOnSuccessListener
you lose that context and therefore you won't be able to call any composable.This is one way to solve it:
This allows you to use it
await
to override callbacks for queries to firebase and any other google api.Read more about how to manage states here