How to Save to Local Storage Using Flutter ?

· 4 min read
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.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply