Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple way to cache cloud firestore documents locally in flutter (not offline persistence)?

I am not referring to firestore offline persistence, but a way to permanently cache documents that will survive closing the database connection and app. I want to cache entire documents.

For example, in a simple chat app. Say there are 100 messages in the conversation, and the user has already read them all. A new message gets sent, so the user opens the app to read the new message. To re-download all 100 messages from firestore will have you charged for 100 document reads. But since the user has already read and retrieved those, I want them cached locally and not read again from the database (since a chat message will never change once created). I understand pagination could help, but I'd rather not read the same static document more than once.

Is SQFlite the best option for this cross-platform, or is there something even better?

like image 890
user11015833 Avatar asked Dec 14 '25 14:12

user11015833


1 Answers

For a chat application I'd typically keep track of what the last message is that the user has already seen. If you show the messages ordered by their timestamp, that means you'll need to keep the timestamp of the latest message they've seen, and its document ID (just in case there are multiple documents with the same timestamp). With those two pieces of information, you can request only new documents from Firestore with collection.startAfter(...).

like image 108
Frank van Puffelen Avatar answered Dec 17 '25 09:12

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!