简体   繁体   中英

How to deal with object from Firebase Realtime Database in Flutter

I am having trouble dealing with object type in realtime database in firebase. I want to convert it into json format. I searched but found none regarding this topic. To be clearer, I am using onValue to listen for this object like this:

currentRef.onValue.listen((event) {
  setState(() {
    currentReading = event.snapshot.value.toString();
  });

But I don't need to convert it to String, I want to deal with data within this Object.

Data printed:

{
"2022 11 21": 400,
  "2022 11 22": 232,
  "2022 11 23": 500
}

Data Type: IdentityMap<String, dynamic>

You can get your specific data this way:

currentRef.onValue.listen((event) {
  String yourSpecificDate = "2022 11 21";
  var result = Map.fromEntries(
        event.snapshot.children.entries.where((element) => element.key == yourSpecificDate));

  print("result = $result");//result = {2022 11 21: 223}
  print("value = ${result.entries.first.value}");//value = 223
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM