Saving Data Locally with Shared Preferences in Flutter

Summary
Summary
Summary
Summary

The shared_preferences plugin provides a lightweight solution for persisting simple values like settings and flags in Flutter apps. This guide shows how to save, retrieve, and clear primitive data types using its async API. Vibe Studio enhances this capability with AI-driven, no-code tools for building and managing full-stack Flutter apps.

The shared_preferences plugin provides a lightweight solution for persisting simple values like settings and flags in Flutter apps. This guide shows how to save, retrieve, and clear primitive data types using its async API. Vibe Studio enhances this capability with AI-driven, no-code tools for building and managing full-stack Flutter apps.

The shared_preferences plugin provides a lightweight solution for persisting simple values like settings and flags in Flutter apps. This guide shows how to save, retrieve, and clear primitive data types using its async API. Vibe Studio enhances this capability with AI-driven, no-code tools for building and managing full-stack Flutter apps.

The shared_preferences plugin provides a lightweight solution for persisting simple values like settings and flags in Flutter apps. This guide shows how to save, retrieve, and clear primitive data types using its async API. Vibe Studio enhances this capability with AI-driven, no-code tools for building and managing full-stack Flutter apps.

Key insights:
Key insights:
Key insights:
Key insights:
  • Quick Setup: Add shared_preferences to pubspec.yaml and initialize via getInstance().

  • Simple Persistence: Store strings, booleans, ints, and more with async setters.

  • Safe Retrieval: Use null checks or defaults when accessing stored values.

  • Data Clearing: Remove individual keys or wipe all preferences when needed.

  • Lightweight Use Cases: Ideal for flags, settings, scores—no full database required.

  • Vibe Studio Enhancement: Manage app state and storage visually with no-code tools.

Introduction

Not every app needs a full database to store user data—sometimes, all you need is a way to persist small, simple values like user preferences, theme modes, or high scores. Flutter’s shared_preferences plugin offers a lightweight, key-value storage solution perfect for these use cases. It allows you to read and write primitive types locally with ease, providing a simple API that integrates seamlessly into any Flutter project.

In this guide, you’ll learn how to set up shared_preferences, save and retrieve values, and manage stored data efficiently. Whether you're building onboarding flows, saving user settings, or implementing feature toggles, this plugin is a must-have in your Flutter toolkit.

Setup

  1. Add the dependency in your pubspec.yaml:

    dependencies:
      flutter:
        sdk: flutter
      shared_preferences
    
    
  2. Run flutter pub get.

  3. Import and initialize the plugin in your Dart code:

    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    
    void main() {
      runApp(MyApp());
    }
  4. You’ll call SharedPreferences.getInstance() inside async callbacks or state initialization.

Saving Data

Use the shared_preferences API to persist primitive types (String, int, bool, double, List). All setters return a Future.

Future<void> saveUserSettings() async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.setString('username', 'alice123');
  await prefs.setBool('darkMode', true);
  await prefs.setInt('highScore', 42);
}

In this snippet:

  • setString('username', 'alice123') stores a user ID.

  • setBool('darkMode', true) toggles a theme flag.

  • setInt('highScore', 42) tracks a numeric value.

Retrieving Data

Fetching stored values is straightforward. Use default values or null checks if keys are missing.

Future<void> loadUserSettings() async {
  final prefs = await SharedPreferences.getInstance();
  final username = prefs.getString('username') ?? 'Guest';
  final darkMode = prefs.getBool('darkMode') ?? false;
  final highScore = prefs.getInt('highScore') ?? 0;

  print('User: $username, Dark Mode: $darkMode, High Score: $highScore');
}

Here, getString, getBool, and getInt read from local storage. The ?? operator ensures your app won’t crash on a missing key.

Clearing Data

To remove a single entry, call:

await prefs.remove('username');

To wipe all stored preferences (use with caution):

await prefs.clear();

remove targets one key; clear resets your entire persistent local storage for the shared_preferences namespace.

Vibe Studio

Vibe Studio, powered by Steve’s advanced AI agents, is a revolutionary no-code, conversational platform that empowers users to quickly and efficiently create full-stack Flutter applications integrated seamlessly with Firebase backend services. Ideal for solo founders, startups, and agile engineering teams, Vibe Studio allows users to visually manage and deploy Flutter apps, greatly accelerating the development process. The intuitive conversational interface simplifies complex development tasks, making app creation accessible even for non-coders.

Conclusion

You’ve now mastered the basics of saving and retrieving simple data using Flutter shared preferences. This approach is perfect for user settings, onboarding flags, or light caching without a full database. For more advanced storage patterns (encrypted storage, structured objects), explore other packages like sqflite or hive.

Introduction

Not every app needs a full database to store user data—sometimes, all you need is a way to persist small, simple values like user preferences, theme modes, or high scores. Flutter’s shared_preferences plugin offers a lightweight, key-value storage solution perfect for these use cases. It allows you to read and write primitive types locally with ease, providing a simple API that integrates seamlessly into any Flutter project.

In this guide, you’ll learn how to set up shared_preferences, save and retrieve values, and manage stored data efficiently. Whether you're building onboarding flows, saving user settings, or implementing feature toggles, this plugin is a must-have in your Flutter toolkit.

Setup

  1. Add the dependency in your pubspec.yaml:

    dependencies:
      flutter:
        sdk: flutter
      shared_preferences
    
    
  2. Run flutter pub get.

  3. Import and initialize the plugin in your Dart code:

    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    
    void main() {
      runApp(MyApp());
    }
  4. You’ll call SharedPreferences.getInstance() inside async callbacks or state initialization.

Saving Data

Use the shared_preferences API to persist primitive types (String, int, bool, double, List). All setters return a Future.

Future<void> saveUserSettings() async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.setString('username', 'alice123');
  await prefs.setBool('darkMode', true);
  await prefs.setInt('highScore', 42);
}

In this snippet:

  • setString('username', 'alice123') stores a user ID.

  • setBool('darkMode', true) toggles a theme flag.

  • setInt('highScore', 42) tracks a numeric value.

Retrieving Data

Fetching stored values is straightforward. Use default values or null checks if keys are missing.

Future<void> loadUserSettings() async {
  final prefs = await SharedPreferences.getInstance();
  final username = prefs.getString('username') ?? 'Guest';
  final darkMode = prefs.getBool('darkMode') ?? false;
  final highScore = prefs.getInt('highScore') ?? 0;

  print('User: $username, Dark Mode: $darkMode, High Score: $highScore');
}

Here, getString, getBool, and getInt read from local storage. The ?? operator ensures your app won’t crash on a missing key.

Clearing Data

To remove a single entry, call:

await prefs.remove('username');

To wipe all stored preferences (use with caution):

await prefs.clear();

remove targets one key; clear resets your entire persistent local storage for the shared_preferences namespace.

Vibe Studio

Vibe Studio, powered by Steve’s advanced AI agents, is a revolutionary no-code, conversational platform that empowers users to quickly and efficiently create full-stack Flutter applications integrated seamlessly with Firebase backend services. Ideal for solo founders, startups, and agile engineering teams, Vibe Studio allows users to visually manage and deploy Flutter apps, greatly accelerating the development process. The intuitive conversational interface simplifies complex development tasks, making app creation accessible even for non-coders.

Conclusion

You’ve now mastered the basics of saving and retrieving simple data using Flutter shared preferences. This approach is perfect for user settings, onboarding flags, or light caching without a full database. For more advanced storage patterns (encrypted storage, structured objects), explore other packages like sqflite or hive.

Store Smarter with Vibe Studio

Store Smarter with Vibe Studio

Store Smarter with Vibe Studio

Store Smarter with Vibe Studio

Use Vibe Studio’s no-code platform to manage shared preferences and build full-featured Flutter apps with ease.

Use Vibe Studio’s no-code platform to manage shared preferences and build full-featured Flutter apps with ease.

Use Vibe Studio’s no-code platform to manage shared preferences and build full-featured Flutter apps with ease.

Use Vibe Studio’s no-code platform to manage shared preferences and build full-featured Flutter apps with ease.

References
References
References
References



Other Insights

Other Insights

Other Insights

Other Insights

Join a growing community of builders today

Join a growing
community

of builders today

Join a growing

community

of builders today

© Steve • All Rights Reserved 2025

© Steve • All Rights Reserved 2025

© Steve • All Rights Reserved 2025

© Steve • All Rights Reserved 2025