FTF Studio
FTF Studio

Reputation: 99

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.

Upvotes: 0

Views: 1406

Answers (3)

Wege
Wege

Reputation: 133

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

Upvotes: 0

Ahmed Raza
Ahmed Raza

Reputation: 540

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.

Upvotes: 0

Mohamed Mohamed
Mohamed Mohamed

Reputation: 61

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

Upvotes: 1

Related Questions