How to save data in flutter? - 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.

Related

If I use firestore offline to store the data for the chat app, do I still need sqlite, flutter

I wanna create a chatapp for my friend, and now remote serve I will use firestore and locally sqlite, but I realized it is default for firestore to save data offline, so my question is I have some data like message, or sending setting for the app, is it ok just to save them in firestore offline which seems automatically and no extra costs.
It depends on what you want or need.
If you want to use the offline persistance only for storing in case internet is not there at all. Firebase supports that out of the box. But you need to keep in mind to do you calls carefully. Because on a slow internet connection, if you tell the Firebase access to wait for the online behavior. It will not be using the local first and you might not be able to show the data. Check the question here
Also if you want to use the offline persistance from Firebase, be mindful that the data size has a default of 40 megabytes of limit. You might assign that by your self. You can check this link
BUT, if you want to have heavy data manipulations and have more control over your data SQLite is more suggested.
My opionion: I think Firebase Offline persistance would be enough for you, so go for it.

how to Store app data and prevent data to get lost and also make app work at background in flutter

I am a intermediate flutter developer and working on my first commercial app And I have almost created whole app ui but problem is when i close my app all data get lost . so how can i prevent it + how can i make my app work at background when app is close like in clock app . In clock app even if app is closed then also we get alarm at the timing we set . It is not possible to provide whole app code thats why i am explaining it in words . Hope you guys will understand the problem and provide best answer .
You can use internal data base concept to save user data like JWT key , user id, first name ,last name or app content for offline usage like blog articles ,pos system data for offline usage
Use Shared preferences to save basic data like user data, JWT but less secure
but if you need save complex data and need query or filter you can use hive and you can keep data more secure
And Alarm is totally deferent approach,
when we schedule alarm,alarm manager schedule task on system alarm services and wakeup app with intent and flutter Isolate class
We divide your question into two parts.
Question 01: How to store data locally :
You can use key-value storage like Shared preferences, Hive
If you are familiar with SQL you can use SQLite
If you are familiar with NoSQL you can use database like sembast
These are some of the popular databases that you can use with flutter. And much more available at pub.dev. But you want to use a database with your task. As an example simply store the user name or user id you can use Shared preferences
Question 02: Make Alarm Application :
For android applications, you can simply use android_alarm_manager
I suggest you take a deep look at the flutter docs about the subject:
Background processes
To run background processes continuously, take a look at this
package: https://pub.dev/packages/background_fetch
If you need to show scheduled messages, use local notifications:
I think by referring to these links you can get an idea.
To store data locally you can use local storage options like Database such as SQFlite, Hive etc or SharedPreference that will save your data locally and to run background process you need to use MethodChannel and write platform specific code or you also have the options to use Isolates. You may read more using this link
https://docs.flutter.dev/development/packages-and-plugins/background-processes

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.

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

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

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.