How to Save to Local Storage Using Flutter

How to Save to Local Storage Using Flutter?

In Native Application, We are having SharedPreferences or Create an SQLite database or even write a file to the device and read it in later. In this article, we will walk through How to Save to Local Storage Using Flutter?

How to Save to Local Storage Using Flutter?

There are few available options which are as listed below:

You can use Shared Preferences from flutter’s official plugins. It uses Shared Preferences for Android, NSUserDefaults for iOS.

Add shared_preferences dependency in your pubspec.yaml

dependencies:

  shared_preferences: any

Users need to store just simple values like API token or login data. Consider a code snippet like below:

import 'package:shared_preferences/shared_preferences.dart';

asyncFunc() async { // Async func to handle Futures easier; or use Future.then
  SharedPreferences prefs = await SharedPreferences.getInstance();
}
...

// Set
prefs.setString('apiToken', token);

// Get
String token = prefs.getString('apiToken');

// Remove
prefs.remove('apiToken');

Shared Preferences use this when storing simple values on storage e.g Color theme, app language, last scroll position. 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.

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 the disk runs out of space.

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 Localstorage

  • Add dependency to pubspec.yaml

    dependencies:
      ...
      localstorage: ^3.0.0
  • Then run the following command

    flutter packages get
  • import the localstorage :

    import 'package:localstorage/localstorage.dart';
  • create an instance

    class MainApp extends StatelessWidget {
      final LocalStorage storage = new LocalStorage('localstorage_app');
      ...
    }
  • Add item to lcoalstorage :

    void addItemsToLocalStorage() {
      storage.setItem('name', 'Abolfazl');
      storage.setItem('family', 'Roshanzamir');
    
      final info = json.encode({'name': 'Darush', 'family': 'Roshanzami'});
      storage.setItem('info', info);
    }
  • Get an item from lcoalstorage:

    void getitemFromLocalStorage() {
      final name = storage.getItem('name'); // Abolfazl
      final family = storage.getItem('family'); // Roshanzamir
      
      Map<String, dynamic> info = json.decode(storage.getItem('info'));
      final info_name=info['name'];
      final info_family=info['family'];
    }
  • Delete an item from local storage :

    void removeItemFromLocalStorage() {
      storage.deleteItem('name');
      storage.deleteItem('family');
      storage.deleteItem('info');
    }

Conclusion:

In this article, we have been through How to Save to Local Storage Using Flutter?

Still, need support for Flutter Development?

Keep Fluttering !!!
Keep Learning !!!

FlutterAgency is a specialized portal focused on Flutter Technology and its developer community. This platform is enriched with numerous Flutter-related resources, such as detailed guides on Flutter Widgets, a variety of Flutter Projects, extensive code libraries, and more.

Abhishek Dhanani

Written by Abhishek Dhanani

Abhishek Dhanani, a skilled software developer with 3+ years of experience, masters Dart, JavaScript, TypeScript, and frameworks like Flutter and NodeJS. Proficient in MySQL, Firebase, and cloud platforms AWS and GCP, he delivers innovative digital solutions.

Leave a comment

Your email address will not be published. Required fields are marked *


ready to get started?

Fill out the form below and we will be in touch soon!

"*" indicates required fields

✓ Valid number ✕ Invalid number