Flutter sharedpreferences: what is it and how to use

What is Flutter Sharedpreferences?

First, we must know what flutter sharedpreferences is. Simply, sharedpreferences are the key or value that stores the data key – value pair in Android and iOS. In Flutter, it uses NSUsersDefaults in iOS and SharedPreferences in Android. Thus, this will provide a persistent store for simple data.

Why should you use Flutter sharedpreferences?

No you might wonder: why you have to use Flutter sharedpreferences. Imagine you want to save a small value, for example, a flag. And you want to refer to it later sometime when a user launches an application. In this case, we can’t use SQLite because you might have to write long codes and supporting classes. It’s very time consuming for such a small value. Then, this is when sharedpreferences come in handy. 

How to use Flutter Sharedpreferences?

One thing to keep in mind is that Flutter DSK does not have supported Sharedpreferences. However, you can use the shared_preferences plugin to persist key – value data on disk

Now, let’s start with implementation

Implementation

First, add the dependencies to  pubspec.yaml file

dependencies:

  flutter:

    sdk: flutter

  shared_preferences: “<newest version>”

After that, import shared_preferences.dart

import ‘package:shared_preferences/shared_preferences.dart’;

Then, it’s time for you to save your date. Remember that we can only add int, String, double and bool with Flutter Sharedpreferences. When it comes to Sharedpreferences, there are 2 setter methods that take 2 parameters, key and value

The final step is reading the data from the storage via Sharedpreferences. In this step, we are only required to pass the key. In case the value is not in the storage, you might get a null value. If you happen to be in this situation, here’s how to solve it

int intValue = prefs.getInt(‘intValue’) ?? 0;

What if you want to remove the data? Here’s the method to do this. But first, you must check if the value presents or not

SharedPreferences prefs = await SharedPreferences.getInstance();

bool CheckValue = prefs.containsKey(‘value’);

If the persistent storage contains the given key, containsKey ( ‘ value ‘ ) will return True. On the other hand, if the given key doesn’t present in the storage, it’ll return False

Public method

Now, let’s moving another way to use flutter sharedpreferences – Public method. 

Contain

First, we must check if the preferences contain a preference by using this key

public abstract boolean contains (String key)

bootlean returns true if there is a preference in the preferences. Otherwise, it will return false

Edit

Now, in case you want to create a new Editor for these preferences. Then, you will be able to make modifications to the data in the preferences and commit those changes to the SharedPreferences object. As that said, here’s the key for this

public abstract SharedPreferences.Editor edit ()

This will return a new instance of the Editor interface. Thus, you can modify the values in this Flutter SharedPreferences object

getAll

This will help you retrieve all the values from preference. However, you mustn’t modify the collection returned with this method or alter any of its content

Here’s its key parameter

public abstract Map<String, ?> getAll ()

This key returns a map which contains a list of key – value pair representing the preferences

Throws

java.lang.NullPointerException

getBoolean

This helps to retrieve a boolean value from the preferences

Key parameter

public abstract boolean getBoolean (String key, 

                boolean defValue)

This key returns the preference value if it exists, or defValue. Throws ClassCastException in case there is a preference with this name that is not a boolean.

Throws

java.lang.ClassCastException

getFloat

When it comes to getFloat, it allows you to retrieve a float value from the preferences

Key parameter

public abstract float getFloat (String key, 

                float defValue)

This key parameter returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a float. In the above example, the key returns defValue, which means the value doesn’t exist

In conclusion

We hope to deliver you the basic understanding about Flutter Spreadpreferences. In case you want to get the best of Flutter Spreadpreferences without any issues or problems, and need help from experts, then, don’t hesitate to contact ArrowHitech. Being one of leading Flutter app development company, we has gained expertise over this emerging technology recently introduced by Google. Working with our decade – long developers, you will get your dream apps with reasonable prices. What are you waiting for? Contact us here

Tags

Share