flutter how to manually push data from firestore and save in sqflite - flutter

I have a firebase cloud database with a document that contains roughly 200 entries (Strings). These entries will only change every few weeks by a bit.
In order to save money (i.e. read and writes in firebase), I would like to push this list once in a while to my app and have it stored in the phone's sqflite database for further use in my app.
Is there a way to "manually" push that list (e.g. once a month) from the cloud database to my app and save it on the device's database? I searched for hours for a solution but nothing seems to fit.
Thanks

Related

Only commit changes to Firestore on app/widget close

As Firestore charges by the read/write, it would be super helpful to keep the changes in memory during the session and only commit them when the user exists either the entire app or a specific section. Is there a way to do that in a Flutter web application?
I think one problem with this approach is that the user might just close the tab including your app. In this case, you have no time to send your data to Firestore.
This aside, you could use packages like Hive to store your documents offline and later run a function to add the data to Firestore later.
You also have 50k reads and 20k writes for free with Firebase, which is sufficient for smaller apps. If you exceed this limit, your app is probably big enough to earn money with it anyway.

Avoid fetching the same data from Firestore over and over again whenever the app starts

I am sorry, if it is a stupid question. I started to work with a Firestore db in a SwiftUI iOS App. Since I will be charged for every read/write/delete I try to avoid to read-in a snapshot with tons of documents (e.g. messages in a messenger app) every time the user starts the app.
I wondering what is the normal way to handle that? Do I have to write/save the messages locally into a sqlite or realm to fetch them at the next time from the internal db (on the Firestore side I create a query to read/load only documents that are newer than the last locally saved message)? Or does the Firestore (Firebase) has a built-in solution e.g. the cache?
I think you've already answered your question. You can save documents (messages) locally so you don't have to fetch them from Firestore every time (and only fetch the new ones). Firebase can cache data locally, but only temporarily. Saving the data using sqlite, realm, or even CoreData is a wise move.

How to save data in flutter?

I am building an app like google's keep (notes taking app) using flutter. I can add notes but when I close the app, it resets to initial state. How can I save these notes so when I come back to app, I can read my previously added notes.
When you're saving in a list for example, and render, you're doing that on UI state which is temporary. If you want to store data to last even after the UI got refreshed, you have to use a database that will hold the data until you delete it.
If you just want to store some tiny data, for example light/dark mode preferences or login/logout sessions, you can use shared_preferences. For larger data, I used sqflite to store data in SQL database in system as a file which will be cleared after the app is memory-cleared or deleted. If you want to store the data in cloud, you can use firebase for that.
You have two options:
1) Save locally on device using database. Below are the db solutions.
Moor
Floor
2) Save on the server, you can use Firebase Cloud Firestore.

addSnapshotListener swift firestore behaviour

I have a document in Cloud firestore to which I listen for updates. It has 2 fields, it has a field description and a field for a picture. The picture is approximately 0.2 mb and description is a few words. I wanted to know what would happen if I made changes to the description in the document, I wanted to know if addSnapshotListener actually downloads a fresh new copy of the document or just the field that has been changed.
I indeed see, by looking at how much data is being downloaded in Xcode, a new fresh copy of the document is downloaded.
This is not efficient at all, since the picture field is rarely changed, only the description might change in my application.
Is there a way to optimize this?
Is there a way to optimize this?
Yes! Don't do that.
Firestore (and the realtime database) is not intended to store images or large datasets per field.
You should explore Storage and keep a reference (url) to the item stored in storage in your Firebase.
Cloud Storage is built for app developers who need to store and serve
user-generated content, such as photos or videos.
By leveraging storage if you need to update or change a field in Firestore, you're only working with a small amount of data instead of an entire image worth.
To answer the question; if you read a document from Firebase, it does read the Document and it's child data.
Here's a link to the Storage Docs which shows how to capture a reference to the item uploaded to storage.
https://firebase.google.com/docs/storage/ios/upload-files
If you want to automatically sync the images to all clients and have them available offline, just put them in a separate document.
// Store your small, frequently changing text here:
db.collection('users').doc(userId).set({email: vince#example.com})
// Store your image here:
db.collection('user_profile_pic').doc(userId).set({data: <imagedata>})

My example app works with adding/viewing data but data not visible in Firestore console

To understand Flutter & Firestore I have used the code from https://heartbeat.fritz.ai/using-firebases-cloud-firestore-in-flutter-79a79ec5303a. The app in a Pixel 2 simulator works as expected ie adding and displaying Tasks. I created a linked to my Firestore database but Tasks added are not being shown in Firestore Data. Any data I add directly in Firestore is not being shown in the app. The obvious conclusion is that I am looking at the wrong Firestore database. But it is from this database I copied the google-services.json and the database Usage relates to when I use the app. What approach can I use to debug this?
The google-services.json firebase_url (line 4) takes me to the firebase - Database - Realtime Database. But the Realtime database displays no data. I then select from the pull-down Firestore (but still no data displayed). I disabled the Realtime database. App works as expected. Displays Tasks stored from previous sessions and I can add new ones. Still can't see any stored data anywhere!!
I seemed to solve my problem by deleting the Pixel 2 emulator and creating a completely new one.