samsa
samsa

Reputation: 78

flutterfire realtime database a children to dynamic list

I am new on flutter. Now i am trying to get a children from realtime database to dynamic list. and with that list i need count them and create box. but when i try that i get all datasnapshot in one box with datakey.

My future class:

Future<dynamic> getdata() async { 
  final ref = FirebaseDatabase.instance.ref("Categories");
  final snapshot = await ref.get();
  if (snapshot.exists) {
       entry.add(snapshot.value);
   print(entry);
  } else {
      print('No data available.');
  } 
}
late List<dynamic> entry =[];

my output

I/flutter (13624): [{-N3lX7tMzVVT7gqUF_1q: deneme, -N3lX9bwkr3X6J3wfHfq: deneme1, -N3lXAwEnNKQ1WcA4Vw2: deneme2, -N3lXFEq5RV5HXkfHiVF: deneme3}]

so i want this one by one not in one line and just data not key. with that i will create box and write there .

Upvotes: 0

Views: 326

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599206

It sounds like you want to loop over the children of the snapshot you get from Firebase, which you can do with:

final ref = FirebaseDatabase.instance.ref("Categories");
final snapshot = await ref.get();
if (snapshot.exists) {
  snapshot.children.forEach((child) { // 👈
            // 👇
    entry.add(child.value);
  })
  print(entry);
} else {
  print('No data available.');
} 

Upvotes: 1

Vishal Thakkar
Vishal Thakkar

Reputation: 682

You have put your snapshot data inside Map like:

Future<dynamic> getdata() async {
      final ref = FirebaseDatabase.instance.ref("Categories");
      final snapshot = await ref.get();
      if (snapshot.exists) {
        Map<dynamic, dynamic> categories = snapshot.value;
        categories.values.forEach((value) {
          print(value);
        });
      } else {
        print('No data available.');
      }
    }

Here you can get all data one by one. (You can use it by adding inside the List).

Upvotes: 1

Related Questions