grizzasd
grizzasd

Reputation: 383

Firebase Realtime Database Date String Range with Dart

In my realtime database I have a series of date strings acting as keys in the format of yyyy-mm-dd. These dates are stored under a user's uid:

enter image description here

I need to get a range of these dates in a sorted order. I can get the dates sorted, but when I try to do a startAt() and endAt() nothing is returned. If I just do a startAt() everything is returned.

This gives me an ordered list:

var query = _database.reference().child(DAY_TABLE).child(UserState.instance.user.uid).orderByValue();

query.onChildAdded.forEach((day) {
    dayList.add(Day.fromJson({day.snapshot.key: day.snapshot.value}));
});

What I'd like to do but doesn't work:

var query = _database.reference().child(DAY_TABLE).child(UserState.instance.user.uid).orderByValue().startAt("2020-09-12").endAt("2020-09-19")

Where the above query would include any and all dates between 2020-09-12 and 2020-09-19. How can I specify such a range?

I thought I might have to convert the date strings to a Unix time, but because I am able to sort the dates correctly, it seems like there should be a way to get this to work. I would prefer to keep them as date strings if it is possible as it is more obvious to work with.

Upvotes: 0

Views: 632

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

You're looking for orderByKey():

_database.reference()
  .child(DAY_TABLE)
  .child(UserState.instance.user.uid)
  .orderByKey()
  .startAt("2020-09-12")
  .endAt("2020-09-19")

Upvotes: 1

Related Questions