How to loop through a Firebase datasnapshot sub childrens? Flutter

后端 未结 2 528
野性不改
野性不改 2021-01-12 21:23

I have a DataSnapshot JSON object :

{fridge2: true, fridge1: true} //data pulled from a real time firebase database

I have to put fri

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 22:09

    I solved it. I used to print(fridgesDs.runtimeType); to get the type of variable returned by firebase. it is actually a HashMap: _InternalLinkedHashMap The casted the returned value into a Map. Finally, I used forEach to loop through the map. Here is the final version:

     Map fridgesDs = snapshot.value['fridges'];
    //    print(fridgesDs.runtimeType);
        fridgesDs.forEach((key, value) {
          if (value) {
            fridges.add(key);
          }
        });
    

提交回复
热议问题