In my Flutter app, users can view products and rate them. The review should be posted to my backend so that we have info about the overall statistics of a product and that sort of things. My question is, how can I in some way cache the request so that when the user rates a product while being offline (or if he has a momentary disconnection), he can continue interacting with other parts of the app and when he is online again the review gets sent in the background?
Here may not be the answer yet since I can not add a comment but I am also looking for the same question. However I found that there are two packages that are helpful:
flutter_offline
(https://pub.dev/packages/flutter_offline): a utility to handle online/offline connectivity
hydrated_bloc
(https://pub.dev/packages/hydrated_bloc): persist and restore bloc state (bloc pattern for state management).
The idea is to create a Hydrated bloc to store all the pending requests, for example, we call PendingRequestsBloc
. The OfflineBuilder
of flutter_offline
is in charge of checking the current connectivity status:
If user rates when online: just send the request as usual, also trigger an event ProcessPendingRequests
to check whether there is any pending request in the PendingRequestBloc
.
If user rates when offline (the main case you are asking about): trigger an event AddPendingRequest
to add the request in your hydrated bloc.
In my opinion, we use hydrated bloc instead of a normal bloc since it can persists its state (which contains the pending requests) even when we close, kill the application.
If you are not familiar with bloc pattern, please visit https://bloclibrary.dev/ for the official documentation. Happy coding!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With