How can I save to local storage using Flutter?

2019-01-07 08:18发布

问题:

In Android if I have 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?

回答1:

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


回答2:

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


回答3:

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.



回答4:

I think If you are going to store large amount of data in local storage you can use sqflite library. It is very easy to setup and I have personally used for some test project and it works fine.

https://github.com/tekartik/sqflite This a tutorial - https://proandroiddev.com/flutter-bookshelf-app-part-2-personal-notes-and-database-integration-a3b47a84c57

If you want to store data in cloud you can use firebase. It is solid service provide by google.

https://firebase.google.com/docs/flutter/setup



标签: flutter