Flutter SharedPreferences Tutorial – Save and Retrieve Data Easily

Flutter SharedPreferences Tutorial – Save and Retrieve Data Easily

When building mobile apps, we often need to store small pieces of data locally, such as login states, user preferences, or simple settings. Flutter provides a lightweight solution for this through the shared_preferences package.

In this blog, we’ll create a reusable utility class to manage all SharedPreferences operations in one place.

shared-pref_flutter


What is SharedPreferences?

SharedPreferences is a key-value storage system in Flutter. It stores small amounts of primitive data (strings, ints, doubles, booleans, and string lists) on the device.

It’s perfect for things like:

  • Saving login status
  • Dark mode settings
  • User preferences
  • Caching small data

Step 1: Add Dependency

Open pubspec.yaml and add:

dependencies:
  shared_preferences: ^2.2.2

Then run:

flutter pub get

Step 2: Create Utility Class

Here’s a SharedPrefUtils class to simplify saving and retrieving values.

import 'package:shared_preferences/shared_preferences.dart';

class SharedPrefUtils {
  // Save boolean
  static Future<bool> saveBool(String key, bool value) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.setBool(key, value);
  }

  // Save integer
  static Future<bool> saveInt(String key, int value) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.setInt(key, value);
  }

  // Save double
  static Future<bool> saveDouble(String key, double value) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.setDouble(key, value);
  }

  // Save string
  static Future<bool> saveString(String key, String value) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.setString(key, value);
  }

  // Save list of strings
  static Future<bool> saveStringList(String key, List<String> value) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.setStringList(key, value);
  }

  // Getters
  static Future<bool?> getBool(String key) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getBool(key);
  }

  static Future<int?> getInt(String key) async {
    final prefs = await SharedPreferences.getInstance();
    final value = prefs.get(key);
    if (value is int) return value;
    if (value is String) return int.tryParse(value);
    return null;
  }

  static Future<double?> getDouble(String key) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getDouble(key);
  }

  static Future<String?> getString(String key) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getString(key);
  }

  static Future<List<String>?> getStringList(String key) async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getStringList(key);
  }

  // Remove single key
  static Future<void> removeData(String key) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove(key);
  }

  // Clear all
  static Future<void> clearData() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.clear();
  }
}

Step 3: Usage Examples

Save Data

await SharedPrefUtils.saveString("username", "JohnDoe");
await SharedPrefUtils.saveBool("isLoggedIn", true);
await SharedPrefUtils.saveInt("age", 25);

Retrieve Data

String? username = await SharedPrefUtils.getString("username");
bool? loggedIn = await SharedPrefUtils.getBool("isLoggedIn");
int? age = await SharedPrefUtils.getInt("age");

print(username);   // JohnDoe
print(loggedIn);   // true
print(age);        // 25

Remove a Key

await SharedPrefUtils.removeData("username");

Clear All

await SharedPrefUtils.clearData();

Best Practices

  • Use SharedPreferences only for small data (settings, flags, tokens).
  • For larger datasets, use SQLite or Hive.
  • Always handle null values when retrieving.

Final Thoughts

With the above utility class, you can easily manage all your SharedPreferences needs in Flutter with clean, reusable methods. This reduces boilerplate code and keeps your app organized.

πŸ‘‰ Next step: Try extending this utility with JSON serialization to save objects.

Post a Comment

Previous Post Next Post