Using SharedPreferences in a better way (Flutter)

Harsh Vardhan Gautam
3 min readJan 14, 2023

If you already came to stop by this article, you must be someone who has already worked in flutter, right?

Without spending much time, let’s dive into the topic straight. For any kind of software application, as developers, we need some way to store certain information on the user’s device. Here, the device can be anything being a browser, mobile, desktop, etc.

With flutter, we know that it supports variety of platforms. To provide developers one unified way to store certain data locally on the user device, flutter provides one package known as shared_preferences.

To get started with shared_preferences, you need to add this as dependency to the pubspec.yml

dependencies:
shared_preferences: <latest_version>

Now, you can use the shared_preferences directly as mentioned in their documentation. But I would like to show you a better way to utilise it.

Let’s do it the better way.

We’ll create one class named LocalStorageService which will encapsulate the behaviour for shared_preferences and we’ll just keep the SharedPreferences instance in the class and consume it wherever we need it.

The class will look something like this.

class LocalStorageService {
static SharedPreferences? instance;

static Future<void> init() async {
instance = await SharedPreferences.getInstance();
}
}

Note: You’ve to call the init method for the above class in the very starting of your app’s lifecycle. It maybe the main method. You just have to ensure that you’re not trying to use the instance before the initialisation.

Now, to use our LocalStorageService , we’ll consume it something like this.

// TO WRITE DATA TO LOCAL STORAGE

// Save an integer value.
await LocalStorageService.instance?.setInt('counter', 10);
// Save a boolean value.
await LocalStorageService.instance?.setBool('repeat', true);
// Save a double value.
await LocalStorageService.instance?.setDouble('decimal', 1.5);
// Save a String value.
await LocalStorageService.instance?.setString('action', 'Start');
// Save a list of strings.
await LocalStorageService.instance?.setStringList('items', <String>['Earth', 'Moon', 'Sun']);
// TO READ DATA FROM LOCAL STORAGE

// Reading data from the 'counter' key.
final int? counter = LocalStorageService.instance?.getInt('counter');
// Reading data from the 'repeat' key.
final bool? repeat = LocalStorageService.instance?.getBool('repeat');
// Reading data from the 'decimal' key.
final double? decimal = LocalStorageService.instance?.getDouble('decimal');
// Reading data from the 'action' key.
final String? action = LocalStorageService.instance?.getString('action');
// Reading data from the 'items' key.
final List<String>? items = LocalStorageService.instance?.getStringList('items');

You may be wondering, what’s the better in this way of using the shared_preferences . Let me tell you that now. If we use the shared_preferences normally, we would be calling await SharedPreferences.getInstance() every single time wherever we want to use it. This becomes quite redundant very quickly in the codebase.

Apart from this, as you can notice that SharedPreferences.getInstance() is asynchronous, so you’ll face some challenges when you want to use it at some place where you’re not inside asynchronous scope.

Thank you for reading all the way through. I really appreciate that you’ve taken some time today for yourself to learn something.

If you learnt something from the article, please give your feedback in comments.

Thank You!

--

--