Flutter sort Firebase snapshot by timestamp

前端 未结 2 722
闹比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条回答
  •  旧时难觅i
    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".

提交回复
热议问题