Step by step to persist data in Flutter

Step by step to persist data in Flutter

By Chanh Ha | Flutter Geeks | 27 Apr 2020


Introduction

Most mobile apps need to store persistence data locally. There are many reasons to do so, from restoring the app state at the last session, to improve app performance. Therefore, Flutter offers us several ways to accomplish this feature. In this post, I will introduce to you 3 different ways to store persistence data in Flutter:

  • Store key-value to shared_preference
  • Saving data to a local database
  • Writing and reading a file

Data Persistence with Shared Preference

I will use shared_preference plugin to store a small amount of data under key-value format.

The most important thing you should remember while using shared_preferences is SMALL DATA. Do not use it for storing large data or complicated data, it will make your code super complicated because of reading and writing this data from shared_preferences.

I can list down here some use cases that shared_preferences is useful:

  • User changes view mode (night mode).
  • Changing the app’s language.
  • Select the app theme.

So when does the data in SharedPreference was clear? The system will erase your data in SharedPreference whenever the user uninstalls your app. Data still remains after the app closes and reopens.

Saving to a local database

To develop an app that needs to persist a large amount of data, structure data, you should consider using a local database. Basically, we can query and write easier.

In Android, developer can use SQLite to serve as a local database. With iOS, it is Core Data. However, we’re still able to use the SQLite database in iOS to avoid the complicated of handling Core Data. In Flutter, we simply use a plugin named sqflite for both iOS and Android app.

Writing and reading a file

In some cases, database can’t fit well your data. You may need to write to a local file and read it later. I can list down here the cases:

  • Save data across app launches.
  • Save log file.
  • Export database as a CSV file

To write and read a file, we use path_provider plugin to get the directory to save file and write file by using dart:io built-in library.

We should note that there are two types of local directory:

  • Temporary directory: It usually use for cache. It can be clear anytime. On iOS, it called NSCachesDirectory. And cache directory in Android.
  • Document directory: A directory that app can store files that only it can read. We should use this type to persist data. It’s named AppData on Android, and NSDocumentDirectory on iOS.

Conclusion

Because the original article is quite long, I can't post it fully here. Please understand my situation. If you feel interested in the topic, I highly recommend you read the full blog post Step by step to persist data in Flutter at Python Geeks. You can get more detail on how to implement step by step and detail explanation.

How do you rate this article?

2



Flutter Geeks
Flutter Geeks

Flutter is Google's UI toolkit for crafting beautiful, natively compiled applications for mobile, web, and desktop from a single codebase. This blog shares to audiences the Flutter tutorial from beginners level to advantage levels.

Publish0x

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.