DEV Community

Mutwoki Dennis Mureti
Mutwoki Dennis Mureti

Posted on

Exploring the Power of Shared Preferences in Flutter Development.

Image description

In this growing technology space of mobile application development, the aim of a developer while developing for mobile applications is efficiency and improved user experience and this can be achieved through a tool known as shared preferences. Shared preferences allow for storage and retrieval of data as key value pairs. We will walk through shared preferences and how it has helped change the game in mobile application development.


What is shared preferences
One of the most interesting data storage options for Android users is shared preferences. Shared preferences is a way developers can store and retrieve date as key/value pairs to a file within the device used to store these data such as String, int, float, Boolean which makes up your preferences in an XML file inside the app on the device storage. Think of shared preferences as a dictionary or key/value pairs. Take an instance where you have a key as “username” and for its value you store the user’s username.

In flutter to get started with shared preferences we use the “shared preferences” package.

**Getting Started

  1. Adding Dependency** To use shared preferences in any of your flutter project you need to add the “shared preferences”package in your “pubspec.yaml” file.

dependencies:
shared_preferences: ^2.0.0

Then run "flutter pub get" for your application to fetch the package.

2. Initialization
This is done by obtaining a instance of “SharedPreferences” using the “getInstance()” method.

import 'package:shared_preferences/shared_preferences.dart';
SharedPrefereences preferences = await SharedPreferences.getInstance();

**Storing and Retrieving data:

  1. Storing Data** To store data in flutter with shared preferences, we get to use the “set” methods. The example below gives an instance of how to Store a string, integer, Boolean and a list of String to item keys.

// String
await preferences.setString(‘username’, ‘flutterFan’);
// integer
await preferences.setInt(‘username’, 20);
// Boolean
await preferences.setBool(‘username’, false);
// list of String to item keys
await preferences.setString(‘username, <String>[‘john’, ‘doe’]);

2. Retrieving Data
You guessed it right. To retrieve data we use the "get" methods. In a case where the requested keys are not found then we provide default values.

String username = preferences.getString(‘username’) ?? ‘DefaultUser’;

**Use Cases in flutter

  1. User Preferences** Shared preferences is exceptional in managing user preferences. Shared preferences provides a convenient solution in managing user preferences, whether it’s for storing theme preferences or language settings.

2. Authentication State
We can effortlessly use shared preferences with persisting authentication state, such as tokens and user credentials.

**Best Practices

  1. Asynchronous operations** Remember that shared preferences are asynchronous so one should always remember to use “await” when fetching or modifying data.

2. Limitations
Shared preferences is suitable for using with small amounts of data. In the case of large data datasets one may consider using other storage solutions such as SQLite databases.

Conclusion
As flutter continues to gain popularity for its expressive User Interface and reactive framework, harnessing and exploring the power of tools like shared preferences becomes an invaluable skill to any developer that puts it into practice. State management in flutter in now easy for you as a developer and to also improve or enhance user experiences.

Happy Coding!!

Top comments (0)