How to save to local storage using Flutter?

In Android, if I have the information I want to persist across sessions I know I can use SharedPreferences or create a SQLite database or even write a file to the device and read it in later.

Is there a way to save and restore data like this just using Flutter? Or would I need to write device-specific code for Android and iOS like in the services example?


There are a few options:

  • Read and write files: https://flutter.io/reading-writing-files/
  • SQLite via a Flutter plugin: https://github.com/tekartik/sqflite
  • SQLCipher via a Flutter plugin: https://github.com/drydart/flutter_sqlcipher
  • SharedPreferences via a Flutter plugin: https://github.com/flutter/plugins/tree/master/packages/shared_preferences
  • Localstore via a Flutter plugin: https://pub.dev/packages/localstore

If you are in a situation where you wanna save a small value that you wanna refer later. then you should store your data as key-value data using shared_preferences

  • Storing key-value data on disk

but if you want to store large data you should go with SQLITE

  • How to get Started with SQLITE in Flutter

however you can always use firebase database which is available offline

  • how to add firebase to your flutter project
  • Firebase for Flutter Codelab from google

Since we are talking about local storage you can always read and write files to the disk

  • Reading and Writing Files

Other solutions :

  • Simple Embedded Application Store database
  • A Flutter plugin to store data in secure storage

A late answer but I hope it will help anyone visiting here later too😁..

I will provide categories to save and their respective best methods...

  1. Shared Preferences Use this when storing simple values on storage e.g Color theme, app language, last scroll position(in reading apps).. these are simple settings that you would want to persist when the app restarts.. You could, however, use this to store large things(Lists, Maps, Images) but that would require serialization and deserialization.. To learn more on this deserialization and serialization go here.
  2. Files This helps a lot when you have data that is defined more by you for example log files, image files and maybe you want to export csv files.. I heard that this type of persistence can be washed by storage cleaners once disk runs out of space.. Am not sure as i have never seen it.. This also can store almost anything but with the help of serialization and deserialization..
  3. Saving to a database This is enormously helpful in data which is a bit complex. And I think this doesn't get washed up by disc cleaners as it is stored in AppData(for android).. In this, your data is stored in an SQLite database. Its plugin is SQFLite. Kinds of data that you might wanna put in here are like everything that can be represented by a database.

You can use shared preferences from flutter's official plugins. https://github.com/flutter/plugins/tree/master/packages/shared_preferences

It uses Shared Preferences for Android, NSUserDefaults for iOS.