tekartik / sembast.dart

Simple io database
BSD 2-Clause "Simplified" License
780 stars 64 forks source link

Pagination not working #378

Closed saravananmnm closed 5 months ago

saravananmnm commented 5 months ago
          @alextekartik 

It was not working as expected. It was returned always first 25 records only, but it has total records 50.

 List<OrdersMasterDTO> orderDetails = [];
pageSize=25;
pageNumber=0;
    var finder = Finder(filter: Filter.equals('orderUploadsStatus', uploadStatus),limit: pageSize??null,offset: pageNumber??null);
    await ordersMasterStore.find(db, finder: finder).then((onData) {
      if (onData != null) {
        for (var items in onData) {
          orderDetails.add(OrdersMasterDTO.fromJson(items.value));
        }
      }
    }).catchError((e) {
      return null;
    });
    return orderDetails;

second call was not working ex: pageNumber =1,pageSize=25. it was retrieved first 25 records always. it was not working as like api call.

@alextekartik could you have any solutions are examples?

Originally posted by @saravananmnm in https://github.com/tekartik/sembast.dart/issues/125#issuecomment-2063454340

alextekartik commented 5 months ago

offset is not a page number put a real index offset so to get the next page you should use offset: 25 in your case.

Here is a simple unit test example that shows that it is working properly:

Future<void> test378() async {
  var db = await newDatabaseFactoryMemory().openDatabase('test.db');
  var store = intMapStoreFactory.store('store1');

  await store.addAll(db, [
    {'value': 1, 'name': 'test1ok'},
    {'value': 1, 'name': 'test2ok'},
    {'value': 0, 'name': 'test3bad'},
    {'value': 1, 'name': 'test4ok'},
  ]);
  Future<List<String>> getNames(int limit, int offset) async {
    return store
        .find(db,
            finder: Finder(
                filter: Filter.equals('value', 1),
                limit: limit,
                offset: offset))
        .then((records) =>
            records.map((record) => record['name'] as String).toList());
  }

  /// Get at offset 0
  expect(await getNames(2, 0), ['test1ok', 'test2ok']);

  /// Get at offset 1 (test1ok first is skipped, test3bad is skipped cause wrong value),
  expect(await getNames(2, 1), ['test2ok', 'test4ok']);

  await db.close();
}
saravananmnm commented 5 months ago

@alextekartik thanks for the solution.

But it's get duplicated.

`Future<List> getNames(int limit, int offset) async { var finder = sem.Finder(filter: sem.Filter.equals('value', 1), limit: limit, offset: offset); return store.find(db, finder: finder).then((records) => records.map((record) => record['name'] as String).toList()); }

for(int i=0;i<5;i++) { print(await getNames(5, i * 5)); }`

12:35:16.511 I [test1ok, test2ok, test3ok, test4ok, test5ok] 12:35:16.518 I [test6ok, test7ok, test8ok, test9ok, test10ok] 12:35:16.523 I [test11ok, test12ok, test13ok, test14ok, test15ok] 12:35:16.530 I [test16ok, test17ok, test18ok, test19ok, test20ok] 12:35:16.536 I [test1ok, test2ok, test3ok, test4ok, test5ok]

ex:- I have total 20 records in db. Each page has 5 records to be display. upto 4th pages it shows correctly. But 5th page it will return again initial 5 records. Actual expectation is empty for 5th page.