简体   繁体   中英

how to apply pagination to firebase search result in flutter

I want to apply pagination to the search result that I get from firebase. to be more clear, I don't want to apply pagination to the listview. I want to get the search result upon the pagination not to get more than one-page data from firebase. and show that to the user.

Firestore/Firebase isn't the best method to apply searching. Try looking at searching with Algolia to begin with.

FirebaseDatabase.getInstance(firebaseApp)
    .getReference("resources")
    .child(FSeason.key)
    .orderByKey()
    .startAt(id)
    .limit(size)

Here, startAt(...) will set the cursor in your database. The order of these data is defined by your orderByKey() function which orders your data alphabetically. Moreover, the limit(...) function defines how many data to retrieve per query.

The id in startAt(id) represents the id of the last retrieved document. So, every time you parse the results from your query, store the last element's id and use that for the next query.

Why don't you try using this package from [vedartm.com]: https://pub.dev/packages/paginate_firestore

PaginateFirestore(
//item builder type is compulsory.
  itemBuilder: (context, documentSnapshots, index) {
    final data = documentSnapshots[index].data() as Map?;
    return ListTile(
      leading: CircleAvatar(child: Icon(Icons.person)),
      title: data == null ? Text('Error in data') : Text(data['name']),
      subtitle: Text(documentSnapshots[index].id),
    );
  },
  // orderBy is compulsory to enable pagination
  query: FirebaseFirestore.instance.collection('users').orderBy('name'),
  //Change types accordingly
  itemBuilderType: PaginateBuilderType.listView,
  // to fetch real-time data
  isLive: true,
),

Just give it a try. You can change the PaginateBuilderType.listView into PaginateBuilderType.gridView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM