vedartm / paginate_firestore

A flutter package to simplify pagination with firestore data 🗃
https://pub.dev/packages/paginate_firestore
MIT License
113 stars 136 forks source link

Add Swipe to refresh or Stream Provider #22

Closed DevyankShaw closed 3 years ago

DevyankShaw commented 4 years ago

Kindly add the swipe to refresh or stream provider in order to get the latest document if any document is added to the collection.

claudemircasa commented 4 years ago

Edit your build method like this:

final GlobalKey<_PaginateFirestoreState> paginateFirestoreState = GlobalKey<_PaginateFirestoreState>();

...

RefreshIndicator(
    child: PaginateFirestore(
        itemBuilder: (context, documentSnapshot) => ListTile(
            leading: CircleAvatar(child: Icon(Icons.person)),
                title: Text(documentSnapshot.data['name']),
                subtitle: Text(documentSnapshot.documentID),
            ),
        // orderBy is compulsary to enable pagination
        query: Firestore.instance.collection('users').orderBy('name'),
    ),
    onRefresh: () async {
        paginateFirestoreState.currentState.refresh();
    },
)

...

Add a new method inside the PaginateFirestoreState class called refresh()

...
void refresh() {
    _bloc..add(PageRefreshed());
}
...

Update method mapEventToState inside PaginationBloc Class:

...
    if (event is PageRefreshed) {
      final firstItems = await _getDocumentSnapshots();
      yield PaginationLoaded(
        documentSnapshots: firstItems,
        hasReachedEnd: firstItems.isEmpty,
      );
      //yield PaginationInitial();
    }
...

it worked for me, but I had to change the code manually

DevyankShaw commented 4 years ago

Thanks but how to access _PaginateFirestoreState class in my dart file? It is private and is in another package.

claudemircasa commented 4 years ago

This is a temporary solution, but in my example I made a copy of the repository and changed it directly in the source code and included the modified version in my project. I modified the _PaginateFirestoreState class to make it visible (PaginateFirestoreState)

DevyankShaw commented 4 years ago

I think if your solution is working then send a pull request as a refresh feature in this repository. So that it can be merged and we all can use this feature.

DevyankShaw commented 4 years ago

Hey, @claudemircasa you finally made the PR. Congrats and Thank you!

But I am interested to know how it is working under the hood. Have you figured out to solve the unnecessary reads problem i.e if there is no new document and the user tries to refresh it repeatedly then it would lead to unnecessary reads?

claudemircasa commented 4 years ago

@DevyankShaw, i believe this is more complicated to resolve. I understand that when the user requests an update of the page it is because he wants to reload all the data again. In order to allow the page to be updated only when you have new documents, it would be necessary to register a listener directly in the Firebase instance and use a Boolean control variable.

DevyankShaw commented 3 years ago

I understand @claudemircasa it would be kind complex to implement. But I think if instead of Swipe to Refresh, Stream Provider is added then the unnecessary reads problem would get solved. What do you think?