flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

Error in query cursors

Open yostrokon opened this issue 3 years ago • 3 comments

https://firebase.google.com/docs/firestore/query-data/query-cursors#use_a_document_snapshot_to_define_the_query_cursor

wrong

db.collection("cities").doc("SF").get().then(
  (documentSnapshot) {
    final biggerThanSf = db
        .collection("cities")
        .orderBy("population")
        .startAt([documentSnapshot]);
  },
  onError: (e) => print("Error: $e"),
);

correct

.startAtDocument(documentSnapshot)

same issue

https://firebase.google.com/docs/firestore/query-data/query-cursors#paginate_a_query // Construct query for first 25 cities, ordered by population

final first = db.collection("cities").orderBy("population").limit(25);

first.get().then(
  (documentSnapshots) {
    // Get the last visible document
    final lastVisible = documentSnapshots.docs[documentSnapshots.size - 1];

    // Construct a new query starting at this document,
    // get the next 25 cities.
    final next = db
        .collection("cities")
        .orderBy("population")
        .startAfter([lastVisible]).limit(25);

    // Use the query for pagination
    // ...
  },
  onError: (e) => print("Error completing: $e"),
);

correct way:

if(documentSnapshots.docs.isNotEmpty){
.
.
.
.
.startAfterDocument(lastVisible).limit(25); 
}

yostrokon avatar Apr 19 '23 07:04 yostrokon

I think either method should be acceptable, but I'll label for further insight from the team.

cc @Lyokone

danagbemava-nc avatar Apr 19 '23 09:04 danagbemava-nc

Using document snapshot like .startAt([documentSnapshot]) cause this error

Error: Invalid argument (dartObject): Could not convert: Instance of '_JsonDocumentSnapshot' dart-sdk/lib/internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49 throw packages/firebase_core_web/src/interop/utils/utils.dart 95:5 jsify packages/cloud_firestore_web/src/interop/utils/utils.dart 42:23 jsify dart-sdk/lib/internal/iterable.dart 415:31 elementAt dart-sdk/lib/internal/iterable.dart 344:26 moveNext dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 781:20 next dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 600:14 of dart-sdk/lib/internal/iterable.dart 214:7 toList packages/cloud_firestore_web/src/interop/firestore.dart 487:22 [_createQueryConstraint] packages/cloud_firestore_web/src/interop/firestore.dart 461:11 startAfter packages/cloud_firestore_web/src/query_web.dart 80:20 [_buildWebQueryWithParameters] packages/cloud_firestore_web/src/query_web.dart 152:46 dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async packages/cloud_firestore_web/src/query_web.dart 149:33 packages/_flutterfire_internals/_flutterfire_internals.dart 112:21 guardWebExceptions packages/cloud_firestore_web/src/internals.dart 12:20 convertWebExceptions packages/cloud_firestore_web/src/query_web.dart 149:12 get packages/cloud_firestore/src/query.dart 392:25 get dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54 runBody dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5 _async packages/cloud_firestore/src/query.dart 390:50 get

yostrokon avatar Apr 19 '23 10:04 yostrokon

The documentation has been updated to reflect the above mentioned changes 👍

russellwheatley avatar Dec 27 '23 12:12 russellwheatley