XtremeDevX
XtremeDevX

Reputation: 1802

Flutter How To Sort Documents In Collection With Field Which Numbers Each Document

My question is how can we sort the documents in a collection, when each document has a field called "Index", which goes from 1 to 10. I thought that the orderBy("Index", descending: false) should sort the documents in the collection going from 1 to 10 in the ascending order. Here is my code, and when I am calling orderBy(), the order of showing of documents in the Cloud Firestore is incorrect, and doesnt show the documents, in any order at all, neither descending or ascending... Thanks for your help and I really appreciate it!

if (petAge >= 42 && petAge <= 56) {
        await petRecordPath.add({
          'Completed': false,
          'Start Due Date': startDateMultiBooster,
          'End Due Date': endDateMultiBooster,
          'Display Range': displayMessageMultiBooster,
          'Importance': 'Vital',
          'Vaccination': 'Puppy Multi Vaccine (DHPP)',
          'Index': 1,
        }).then((value) async {
          await petRecordPath.add({
            'Completed': false,
            'Start Due Date': startDateMultiBooster,
            'End Due Date': endDateMultiBooster,
            'Display Range': displayMessageMultiBooster,
            'Importance': 'Recommended',
            'Vaccination': 'Leptospirosis Vaccine',
            'Index': 2,
          }).then((value) async {
            await petRecordPath.add({
              'Completed': false,
              'Start Due Date': startDateMultiBooster,
              'End Due Date': endDateMultiBooster,
              'Display Range': displayMessageMultiBooster,
              'Importance': 'Recommended',
              'Vaccination': 'Deworm 1',
              'Index': 3,
            });
          });
        }).whenComplete(() {
          petRecordPath.orderBy('Index', descending: false);
        });
      }

Here is how it looks in the Firebase console:

First Document:

enter image description here

Second Document:

enter image description here

Third Document:

enter image description here

Finally, with the build in sort feature from the console itself:

enter image description here

Upvotes: 2

Views: 1733

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600126

The Firestore console doesn't automatically order the documents, and is not affect in any way by the orderBy() call that your code makes. In fact, that would mean that any user could make API calls that change how your console displays the data, which sounds like a bad idea.

Only when you specify a sort order in the console (like in your last screenshot) does it show the documents in that order.

This is similar to how the API works: unless you specify a sort order when reading the documents, they will be retrieved in an undefined order. And your petRecordPath.orderBy('Index', descending: false) would ensure that the documents are read by your code in the specified order.

Upvotes: 3

Related Questions