Flutter sort Firebase snapshot by timestamp

前端 未结 2 679
闹比i
闹比i 2021-01-18 20:11

I\'m trying to sort snapshot by timestamp but returns original order.
data structure looks like this

I have two snapshot and timestamps are -153602546653

相关标签:
2条回答
  • 2021-01-18 20:31

    From the above code, it looks like you are not having a variable to hold the list.

     Map<dynamic, dynamic> map = {
        "one": {"timestamp": 1},
        "two": {"timestamp": 2}
      };
      List list = map.values.toList(); //variable which holds new list created from Map.
                                       //As it new list, any change in list will not have no impact on map.
      list.sort((a, b) {
        return b["timestamp"].compareTo(a["timestamp"]);
      }); // inplace sort
      print(list); // [{timestamp: 2}, {timestamp: 1}]
    

    If you want key also in the result,

     var list = map.entries.toList();
     list.sort((a, b) => b.value["timestamp"].compareTo(a.value["timestamp"]));
     var list2 = list.map((a) => {a.key: a.value}).toList();
     print(list2); // [{two: {timestamp: 2}}, {one: {timestamp: 1}}]
    
    0 讨论(0)
  • 2021-01-18 20:38

    Unfortunately when you call snapshot.data?.snapshot?.value, the resulting Map doesn't have space for the order of the items anymore.

    In most Firebase SDKs you can solve this by looping over the children of the snapshot in the value event listener. But in Flutter you will need to listen to onChildAdded events to maintain the value of the items in Flutter, at least until this issue is addressed: https://github.com/flutter/flutter/issues/20745.

    From one of my own projects:

    ref.orderByKey().equalTo("child1").onChildAdded.listen((event) {
      print(event.snapshot.key);
    });
    ref.child("child1").once().then((snapshot) {
      print("Done loading all data for child1");
    });
    

    This will first print the keys of all child nodes (in the requested order), and then "Done loading all data for child1".

    0 讨论(0)
提交回复
热议问题