DEV Community

Cover image for How To Store And Get Data From Shared Preferences in Flutter - fluttercorner.com
FlutterCorner
FlutterCorner

Posted on

How To Store And Get Data From Shared Preferences in Flutter - fluttercorner.com

Hello Guys How are you all? Hope you all are fine. Today We are going to learn How To Store And Get Data From Shared Preferences in Flutter?

We have to store some offline data to our storage and get them back while we want in shared preferences in flutter. In This tutorial very simple way I am going to explain How To Store And Get Data From Shared Preferences in Flutter. So lets start this tutorial.

How To Store And Get Data From Shared Preferences in Flutter

We are going to use flutter package for shared preferences.
define shared_preferences package in pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^0.5.12
Enter fullscreen mode Exit fullscreen mode

Then make StorageUtil.dart file in your lib folder.

Then after import shared_preference package in our file.

import 'package:shared_preferences/shared_preferences.dart';
Enter fullscreen mode Exit fullscreen mode

Make class named StorageUtil in StorageUtil.dart file.

After that, define StorageUtil and SharedPreferences variables.

class StorageUtil {
  static StorageUtil _storageUtil;
  static SharedPreferences _preferences;
}
Enter fullscreen mode Exit fullscreen mode

Actually Shared Preference is using async and await method. so it will use Future to put and get from Shared preference.

Make getInstance method to get Storage and set this method to init so it will init every time.

  static Future<StorageUtil> getInstance() async {
    if (_storageUtil == null) {
      var secureStorage = StorageUtil._();
      await secureStorage._init();
      _storageUtil = secureStorage;
    }
    return _storageUtil;
  }

  StorageUtil._();
  Future _init() async {
    _preferences = await SharedPreferences.getInstance();
  }
Enter fullscreen mode Exit fullscreen mode

Then I Have Created Method to put data into Preference. so you can use this method.

  // put string
  static Future<bool> putString(String key, String value) {
    if (_preferences == null) return null;
    return _preferences.setString(key, value);
  }
I have also created method to get data from Preference.
  // get string
  static String getString(String key, {String defValue = ''}) {
    if (_preferences == null) return defValue;
    return _preferences.getString(key) ?? defValue;
  }
Also I Have Method that will clear data from Preference.
  // clear string
  static Future<bool> clrString() {
    SharedPreferences prefs = _preferences;
    prefs.clear();
  }
Enter fullscreen mode Exit fullscreen mode

Now, Our StorageUtil.dart is Ready. Lets move to our main.dart file.

First of all lets import StorageUtil.dart and material.dart in our main.dart file.

import 'package:flutter/material.dart';
import 'package:flutter_app_test/StorageUtills.dart';
Then, make main void method. But Wait for one minute. We have to initialize our StorageUtil In our main void method.
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await StorageUtil.getInstance();
  runApp(SharedPreference());
}
Enter fullscreen mode Exit fullscreen mode

Then make StatefulWidget class named SharedPreference that we define in runApp.

class SharedPreference extends StatefulWidget {
  @override
  _SharedPreferenceState createState() => _SharedPreferenceState();
}

class _SharedPreferenceState extends State<SharedPreference> {

}
Enter fullscreen mode Exit fullscreen mode

Now, when ever you want to set data into your Preference then just paste this code.

StorageUtil.putString("key_name", "This is Key Value");
Enter fullscreen mode Exit fullscreen mode

In Above line there is first value is our key that we will use to store and receive data. and second one is key value.

Now, how to get key value ? Its also very simple. Just Paste this code where ever you want value.

StorageUtil.getString("key_name");
Enter fullscreen mode Exit fullscreen mode

just define key_name. It will return value.
Here is my main.dart file Source. Here is How To Store And Get Data From Shared Preferences in Flutter

Top comments (0)