Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 265
Reagankm Avatar asked Oct 08 '22 13:10

Reagankm


People also ask

How do I store JSON data in local storage in Flutter?

Simple example fromJson(Map<String, dynamic> json) : this. value = json['value']; Map<String, dynamic> toJson() => {'value': value}; } ... JsonStore jsonStore = JsonStore(); CounterModel counter; loadFromStorage() async { Map<String, dynamic> json = await jsonStore. getItem('counter'); counter = json !=

How do you save to external storage in Flutter?

In Android, to write a file to external storage, the application needs to be granted WRITE_EXTERNAL_STORAGE permission. That permission also grants the application READ_EXTERNAL_STORAGE permission. We also need to add permission in AndroidManifest.


2 Answers

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
like image 157
Seth Ladd Avatar answered Oct 16 '22 07:10

Seth Ladd


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
like image 79
Raouf Rahiche Avatar answered Oct 16 '22 08:10

Raouf Rahiche