how to Store app data and prevent data to get lost and also make app work at background in flutter - 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

Related

How to send data from background service to main application in flutter

I am running a background servie and at a specific time, I want to send some data in form of JSON string to my application. It may happen that my app is currently terminated, so I am using app laucher plugin to open my application. After opening the application, I want to send some data to application so as to display it. So, how can I send the message and recieve it in my main application (I will have to change the state of my widget once I recieve the data)?
I am not completely sure if this works, but I think instead of somehow sending the data from the background service to the main application you could probably save it in shared preferences or local database or using hive or something similar and simply retrieve it in your main application.
Definitely agree with above answer , But if you want to secure your application more then you can use Flutter_Secure_Storage. It is alternative to Shared_Preference :)

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.

Is there a better way to handle user sessions in Flutter, than using "flutter_session" or "local_storage", or is impossible to handle that on mobile?

Do we have to handle them from the back-end only? Because those two libraries to me they act as storage, they not doing what I am expecting.
Do I have to send a request to the back-end every time I want to resume my app state to renew the token, if the app was on foreground for some period of time?
I feel by far the best way to handle sessions in flutter is by using Shared Preferences
Link to the package:
https://pub.dev/packages/shared_preferences
It can be used to create multiple tokens or flag which can be said as cookie equivalents of a website.
I think you will figure out the working by looking at the documentation or the example, it is fairly simple
For more sensitive data you may be better to use Secure Storage which will ensure that the data is encrypted. Shared preferences stores the data that is not encrypted

flutter data storage: local storage vs cloud storage

a question about local and remote storage of user data. Is there a best practices for the common situation where a user accesses data from an API and can favourite or otherwise personalise the data.
I have seen tutorials, e.g. a movie browsing app, where the use can make a list of favourite movies, where this personalised data is stored locally (e.g. in sqflite) and other tutorials where this data is stored remotely, eg. firebase. And firebase has an offline mode, so that data can be synced later. In that case, is it a common use case to set up local storage as well as cloud storage? Is there a common practice for this situation?
Thanks for any insights.
This is not specifically a Flutter question, more of a general app development question. It's very common to have both local and cloud "storage" but I wouldn't think of it that way. If you're interacting with an API backend I wouldn't consider it as the cloud storage for your app. Instead look at it as a different component within your applications overall architecture. You API/Backend component, this way it's not apart of your app instead it's something your app interacts with.
I assume you know the purpose of your API. Returns your data you want to see, keeps track of user profile information and other sensitive information.
When it comes to local storage I'd say the most common scenarios for local storage is results caching and storing information that the API requires on every session to make the user experience a bit better. See some examples below for both:
On instagram they store your "Feed watermark" which is a string value that is linked to a specific set of results so that when you open the app and request again they return that set of results, plus anything new - Local storage
They also "store locally" (better referred to as caching) a small set of your feeds from your posts, a list of user profiles that has stories on them and your DM's for instant and offline access. This way when the app loads up it has something to show while performing the action to get the new information. - Caching
They also store your login token, that never expires. - Local storage
tl;dr: Yes. If you need data on every session to use your API store that locally in a secure way and use that to interact with your "Cloud storage".

How and which database to use with NWjs offline windows app to store data

i made a simplest application in nwjs which just takes an input of name .
now i can't store this value permanently because app is not connected with any database .
i know about mongodb but don't know how to integrate with javascript app.
i watched some tutorials how to use mongodb with apps but they in tutorials always use cmd to first start server (now a general software don't require , user to start servers manually etc).
Please help me if there is a way to store, fetch and perform other operations on data offline.
what i am missing ? Thanks
You may be looking for:
NeDB, a Mongo-like offline in-memory database
LinvoDB3, same than NeDB, but not in-memory (slower, but more scalable in terms of size)
LocalStorage a Web API