I am attempting to load data from a Firebase DataSnapshot, but I don't receive any data when trying this:
override fun onActive() {
super.onActive()
FirebaseDatabase.getInstance()
.getReference("/users")
.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onCancelled(databaseError: DatabaseError) {
Log.d("UserRepository","databaseError ${databaseError.message}")
}
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists()) {
value = dataSnapshot.children
}
}
})
}
But with the code above I do not receive any data in dataSnapshot
DataSnapshot
has getChildren()
which contains data snapshot instances, all you need to do is using a for loop in order to be able to access all DataSnapshot
instances to get YourModelClass
class by using getValue()
.then add them into the model list
for(DataSnapshot ds : dataSnapshot.getChildren()) {
YourModelClass modelObject = ds.getValue(YourModelClass.class);
Log.d("TAG", modelObject.getMsg());
modelList.add(modelObject)
}