0

The list shows usernames on the console when I use the code below

Log.d("read data", "Yay!${bal.displayName}")

But when I add the list to the LazyColumn, it doesn't display anything, except the single items I add atop and below the list to check if it was showing at all. Below is my code

  @Composable
    fun UsersList(userInf: List<UserInfo.Uids>) {
        Scaffold {
            LazyColumn{
               items(userInf) {users ->
                   users.displayName?.let {
                Text(it)
           }
                }

            }
        }

    }

Firebase Query I got the code from the official website for Firebase query here

 ref.addChildEventListener(object : ChildEventListener {
            override fun onChildAdded(snapshot: DataSnapshot, previousChildName: String?) {
                for (dataSnapshot in snapshot.children) {
                    val bal = snapshot.getValue<UserInfo.Uids>()
               if (bal != null) {
                    balList.add(bal)
                    Log.d("read data", "Yay!${bal.displayName}")
                }}
               

               


            }

})

In conclusion, Data is read from the database but, not displayed on LazyColumn in Compose

UPDATE

This is how balList is instantiated

var balList = ArrayList<UserInfo.Uids>()

This is the data class

data class UserInfo(
        val uid: Uids) {
        data class Uids(
            var displayName: String? = null,
        )
    }

Additionally, I realised that the value added to balList from the firebase query doesn't keep its value outside the query functions, seems it has something to do with states

Raphael Inyang
  • 173
  • 1
  • 4
  • 13
  • Your code is not full, update it to [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Phil Dukhov Apr 11 '22 at 15:34
  • @PylypDukhov please, what more of my code do you need to see, i shortened the query code because the rest is not needed for this problem, the data class only contains one item which is the displayname – Raphael Inyang Apr 11 '22 at 16:18
  • 2
    What is `balList`, how is it stored, is it stored in the view model, how is it passed to `UsersList`? [MRE](https://stackoverflow.com/help/minimal-reproducible-example) means that I can run your code and reproduce the problem, this increases your chances to get an answer a lot. – Phil Dukhov Apr 12 '22 at 03:01
  • Please edit your question and add the information Pylyp asked for, and please also respond with @. – Alex Mamo Apr 12 '22 at 05:20
  • @PylypDukhov I have updated the question, with the minimal example and the required variables – Raphael Inyang Apr 12 '22 at 08:46
  • 1
    Compose can only track changes from mutable state holder values, it's a special type created by Compose. Use `mutableStateListOf` instead of `ArrayList`. I suggest you start with state in Compose [documentation](https://developer.android.com/jetpack/compose/state), including [this youtube video](https://youtu.be/mymWGMy9pYI) which explains the basic principles. Also make `val displayName` instead of `var` to prevent error in future - update it with `copy` instead. – Phil Dukhov Apr 12 '22 at 08:49
  • ```copy``` instead of ```add``` in the firebase query? – Raphael Inyang Apr 12 '22 at 08:50
  • 1
    There is no way you can simply use the `balList` outside the callback. Firebase API is asynchronous. So please check the duplicate to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Firebase Realtime Database using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Apr 12 '22 at 09:03
  • It worked, thanks @PylypDukhov , but an item in the list appears more than once – Raphael Inyang Apr 12 '22 at 09:05
  • I solved it. using ``` for (dataSnapshot in snapshot.children) {} ``` makes an item appear more than once – Raphael Inyang Apr 12 '22 at 09:16

0 Answers0