How to Get All Child's Data in Firebase Database

How to get all child's data in firebase database?

First retrieve your users datasnapshot.

//Get datasnapshot at your "users" root node
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Get map of users in datasnapshot
collectPhoneNumbers((Map<String,Object>) dataSnapshot.getValue());
}

@Override
public void onCancelled(DatabaseError databaseError) {
//handle databaseError
}
});

Then loop through users, accessing their map and collecting the phone field.

private void collectPhoneNumbers(Map<String,Object> users) {

ArrayList<Long> phoneNumbers = new ArrayList<>();

//iterate through each user, ignoring their UID
for (Map.Entry<String, Object> entry : users.entrySet()){

//Get user map
Map singleUser = (Map) entry.getValue();
//Get phone field and append to list
phoneNumbers.add((Long) singleUser.get("phone"));
}

System.out.println(phoneNumbers.toString());
}

This listener will only retrieve the datasnapshot when explicitly called. Consider storing a list of numbers under an "allPhoneNumbers" node in addition to your users node. This will make your datasnapshots lighter and easier to process when collecting all numbers. If you have say hundreds of users, the "users" datasnapshot will be way too big and the "allPhoneNumbers" list will be far more efficient.

The above code was tested on your sample database and does work. However, your phone field may need to be casted to String or int depending on your user's phone instance field's type.

How to get all child data on nested firebase data in Flutter?

When structuring the database, you need to take into account that it has to be flat structure. If you change the structure to your second image, then you can do the following:

final FirebaseDatabase _database = FirebaseDatabase.instance;
User currentUser = FirebaseAuth.instance.currentUser;

var ref = _database.reference().child(currentUser.uid);
ref.once().then((DataSnapshot snapshot){
print(snapshot.value);
print(snapshot.key);
snapshot.value.forEach((key,values) {
print(values);
});
});

Here first get an instance of the database and get the currently login user, then using once() you can fetch the data, to be able to access the fields that are nested then you have to use forEach() to iterate inside the datasnapshot.

how to get all child nodes from firebase database?

Try the following:

databaseReference = FirebaseDatabase.getInstance().getReference("Users");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Userlist = new ArrayList<String>();
// Result will be holded Here

for (DataSnapshot dsp : dataSnapshot.getChildren()) {
String userkey = dsp.getKey();
String scores = dsp.child("scorehigh").getValue().toString();
Userlist.add(userkey); //add result into array list
}
}

un.setText(Userlist.get(2));

}

@Override
public void onCancelled(DatabaseError error) {

}
});

The reference is at Users then you need to loop twice to be able to access the attributes scorehigh and username

How to get all child keys in Firebase?

Use Firebase's shallow query https://firebase.google.com/docs/database/rest/retrieve-data#shallow

For example, if your data is structured like this

parent: {
child_0: {...<large data>...},
child_1: {...<large data>...},
child_2: "abc",
child_3: 123
}

The shallow query's result will be:

parent: {
child_0: true,
child_1: true,
child_2: "abc",
child_3: 123
}

Flutter: How to fetch (all) of a child data from firebase realtime database in streambuilder/futurebuilder with ListViewBuilder?

This code in your app:

final userS = snapshot.data!.snapshot.value

You're taking the value of the DataSnapshot object, which in your case is going to be a Map<String, dynamic>. And a Map doesn't have an [] accessor, which is why you get an error.

If you want to get the results by index, it's probably easier to keep the DataSnapshot:

final userS = snapshot.data!.snapshot;

And then access its children by their index:

children: [
Text(userS.children.toList()[i].child('username').value),
Text(userS.children.toList()[i].child('password').value)
],

So here we use userS.children.toList() to get the child snapshots into a list, and then look up the correct snapshot by its index. From there we can then get the value of its username and password child properties.

android firebase database how to retrieve all data from child node

You're creating a reference with this:

database = FirebaseDatabase.getInstance();
myRef = database.getReference().child("Sold").child("Item");

And then you add a ChildEventListener. That means that the DataSnapshot in your onChildAdded will be called with a snapshot of the DUMMY 1 node first, and then with a snapshot of the DUMMY 2 node.

You're calling dataSnapshot.getValue(String.class) in onChildAdded. But since DUMMY 1 and DUMMY 2 have multiple properties, they don't have a single string value. So that call returns null, which is probably what's causing you problems.

If you want to add the DUMMY 1, DUMMY 2 key itself to the adapter, you can call dataSnapshot.getKey(). Otherwise you can get the value of the specific child properties with dataSnapshot.child("Description").getValue(String.class).

So:

myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String key = dataSnapshot.getKey();
String description = dataSnapshot.child("Description").getValue(String.class);
Long quantity = dataSnapshot.child("Quantity").getValue(Long.class);
myArrayList.add(key+": "+description+" ("+quantity+")");
myArrayAdapter.notifyDataSetChanged();
}

If you'd like to read the entire value into a class representing the item (a so-called POJO), the simplest class is:

public class Item {
public String Description;
public Long Quantity;
}

Note that the name (including the case) of the fields must exactly match the names of the properties in your database. You can use the above class like this:

myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String key = dataSnapshot.getKey();
Item item = dataSnapshot.getValue(Item.class);
myArrayList.add(key+": "+item.description+" ("+item.quantity+")");
myArrayAdapter.notifyDataSetChanged();
}


Related Topics



Leave a reply



Submit