A Flutter Class for Your App’s Preferences

A Walkthrough

The Class Library, prefs.dart, was first introduced in the article, Store and Read Your App’s Preferences, and will be reviewed here in this article. It’s a utility class. Its API is a list of static properties and static methods. We’ll work ‘top-down’ and examine how this class library is implemented.

Image for post
Image for post
Other Stories by Greg Perry

In the Beginning

The beginning of this class library, you will see the appropriate import statements, init() function and dispose() function. By design, the init() function is to be called in the State object’s initState() function — the dispose() function called in the State object’s dispose() function.

Note, the static properties, _prefs and _prefsInstance. They’re ‘library-private’ variables. The first takes a reference of the class, SharedPreferences, of a type, Future<SharedPreferences>. There, we’re getting a reference to the plugin, ‘plugins.flutter.io/shared_preferences’. The next variable, _prefsInstance, is assigned an instance of the class, SharedPreferences, if and when the init() function is called. I’m implying here that calling the init() function is indeed optional. As is the called in of the dispose() function. There’s a class variable, _initCalled, that determines that fact. Making it optional allows implementations that possibly don’t allow you to call the init() function to be used and yet still allow the class library to function.

Image for post
Image for post

Here’s the Key

The next two functions in the class library involves the ‘preference keys’. When working with your app, assigning and storing your preferences, you will have a collection of ‘keys’ used to look up those preferences. The first function will return a ‘Set of Strings’ listing those keys while the second function returns its ‘Future’ version. Note, the assert statements in the first function, getKeys(). This means, if you don’t want to deal with ‘Future’ data types, you must call the init() function somewhere before using this class library.

Image for post
Image for post

I Get It!

The last two functions above allow you to simply get a preference by its key without being concern with that preferences data type. The return data type for these functions is dynamic, and so the preference value will be returned in its own data type. Note, if the key doesn’t exist, a value of null is returned.

‘F’ is for Future

This class library as well as SharedPreferences library deals with ‘Future’ data types. You’ll see in this class library that those functions that return a ‘Future’ data type has the naming convention with a capital ‘F’ append on the end of the function’s name.

For Instance!

You’ll realize, within each async ‘get’ function in this class library that returns a ‘Future’ data type, you’re going to see one line again and again:

if (_prefsInstance == null) {

If there’s an ‘instance’ of SharedPreferences, the code is going to take advantage of it to retrieve the preference value — the init() function was called and instantiated an instance of SharedPreferences. Otherwise, the ‘await’ command is called again and again to get a required instance of the SharedPreferences:

var instance = await _prefs;

Since your app will be dealing with a finite list of preferences in most cases (typically when your app first starts up), the latency using the ‘await’ command will be tolerable. Try it (don’t call the init() function) and see for yourself.

Boolean

Supply a key string to these two functions, and you’ll get a Boolean value. If no key is found, a value of ‘false’ is returned.

Image for post
Image for post

Int

You’re going to see a pattern here with all these ‘get’ functions. The first function, getInt(), will return an int data type. Unless, of course, there’s not instance of SharedPreferences. While developing, the assert statements will trigger. In the second function, if there’s an instance of SharedPreferences, you see the first function is instead called. If there’s no such key, the value of zero is returned.

Image for post
Image for post

Double

Returns the number, 0.0, if the key is not found.

Image for post
Image for post

String

Returns ‘the empty string’ if the key is not found.

Image for post
Image for post

StringList

A List of Strings are returned with these two functions. A empty string in the List if the key’s not found.

Image for post
Image for post

Set Your Preference

When setting a preference, in fact, you don’t need to call the init() function first as the ‘await’ command is utilized every time to get an instance of SharedPreferences. I felt, since these ‘set’ functions are time-consuming operation anyway returning a ‘Future’ data type, most developers would not want ‘to wait’ for the Boolean result and would instead just call it and carry on:

setInt(‘month’, 10);

Further, these ‘set’ functions will not be called very often. A user, when using your app, will normally set their preferences maybe once or twice and then forget it. This further allows for this implementation.

Image for post
Image for post

Remove & Clear

Finally, the class library allows you to remove a particular preference by its key if you must. And if you want to get drastic, you can clean up by calling the clear() function removing all the preferences. Both return a Future object of type Boolean.

Image for post
Image for post

Written by

Freelance Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store