Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Get Firebase Database reference child all data

I have a firebase database child which content is like below:

enter image description here

How to retrieve this in flutter?

My Current code:

static Future<Query> queryUsers() async{
return FirebaseDatabase.instance
    .reference()
    .child("zoom_users")
    .orderByChild('name');
}

queryUsers().then((query){
  query.once().then((snapshot){
    //Now how to retrive this snapshot? It's value data giving a json
  });
});
like image 827
Yeahia2508 Avatar asked Nov 29 '22 21:11

Yeahia2508


1 Answers

To retrieve the data try the following:

db = FirebaseDatabase.instance.reference().child("zoom_users");
db.once().then((DataSnapshot snapshot){
  Map<dynamic, dynamic> values = snapshot.value;
     values.forEach((key,values) {
      print(values["name"]);
    });
 });

Here first you add the reference at child zoom_users, then since value returns data['value'] you are able to assign it to Map<dynamic, dynamic> and then you loop inside the map using forEach and retrieve the values, example name.

Check this:

https://api.dartlang.org/stable/2.0.0/dart-core/Map/operator_get.html

Flutter: The method forEach isn't defined for the class DataSnapshot

like image 168
Peter Haddad Avatar answered Dec 04 '22 12:12

Peter Haddad