Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
  • Flutter Apps
Menu
Benefits of shared_preferences and secure_storage in Flutter

Benefits of shared_preferences and secure_storage in Flutter

Posted on July 1, 2024 by Toma Velev

In Flutter, shared_preferences and flutter_secure_storage are two popular packages used very often for persisting data locally on a device. They have similar interface – key-value storage, but the second is protected by the OS Security. 

Shared Preferences

shared_preferences is used for storing simple data as key-value pairs. This is similar to storing data in a dictionary or using localStorage in web development. It is typically used for non-sensitive data.

Benefits of Shared Preferences:

  1. Simplicity: Easy to use for storing basic data types such as strings, integers, doubles, and booleans.
  2. Persistence: Data stored using shared_preferences persists across app launches, meaning it is saved on the device even when the app is closed and reopened.
  3. Quick Access: Fast read and write operations, suitable for small amounts of data.
  4. Shared Data: Data can be shared across the entire application.
  5. No Extra Permissions: Typically doesn’t require special permissions to store data.

Typical Use Cases:

  • Storing user preferences (e.g., theme settings, language choice).
  • Caching simple state information.
  • Storing basic configuration options.

Secure Storage

flutter_secure_storage is used for storing sensitive data securely. It uses platform-specific secure storage solutions, such as Keychain on iOS and EncryptedSharedPreferences on Android.

Benefits of Secure Storage:

  1. Encryption: Provides encryption to ensure that the stored data is secure and cannot be easily accessed or tampered with.
  2. Security: Suitable for storing sensitive information like authentication tokens, passwords, or any other confidential data.
  3. Platform Integration: Utilizes the underlying platform’s secure storage mechanisms, ensuring that data is managed according to platform-specific security practices.
  4. Device Protection: Data is protected even if the device is compromised, providing an additional layer of security.

Typical Use Cases:

  • Storing user credentials (e.g., OAuth tokens, passwords).
  • Storing sensitive configuration data.
  • Storing private keys or any other sensitive information that requires encryption.

Comparison:

  • Data Sensitivity: Use shared_preferences for non-sensitive data and flutter_secure_storage for sensitive data.
  • Ease of Use: shared_preferences is simpler to implement for straightforward key-value storage, while flutter_secure_storage requires more setup but provides higher security.
  • Security: flutter_secure_storage offers encrypted storage, making it suitable for confidential data, whereas shared_preferences does not offer encryption.

Example Usage:

Shared Preferences:

import 'package:shared_preferences/shared_preferences.dart';

void savePreference() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  await prefs.setString('username', 'exampleUser');
}

void getPreference() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  String? username = prefs.getString('username');
  print('Username: $username');
}

Secure Storage:

import 'package:flutter_secure_storage/flutter_secure_storage.dart';

final storage = new FlutterSecureStorage();

void saveSecureData() async {
  await storage.write(key: 'token', value: 'secureToken');
}

void getSecureData() async {
  String? token = await storage.read(key: 'token');
  print('Token: $token');
}

Conclusion:

Using shared_preferences and flutter_secure_storage together allows you to handle a variety of data persistence needs in a Flutter app. shared_preferences is ideal for non-sensitive data where ease of use and performance are key, while flutter_secure_storage is crucial for storing sensitive information securely. For more Flutter Tips – check out my blog: https://programtom.com/dev/?s=flutter

  • What are ways to Optimize the backend endpoints in Spring Boot
  • Flutter image flickers
  • Could a Flutter App save a Flag even after uninstall
  • Could iOS flutter app logs be viewed while running in release mode – started after previous closed state
  • 6 Addictive Mobile Game Ideas Inspired by Flappy Bird’s Simplicity

Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (206)
  • Graphical User Interface (13)
  • Marketing (114)
  • Software Development (270)
  • Spring (43)
  • StartUp (21)
  • Uncategorized (4)
  • Uncategorized (15)
  • Vaadin (14)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (83)
    • Flutter Apps (23)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • What are ways to Optimize the backend endpoints in Spring Boot
  • Flutter image flickers
  • Could a Flutter App save a Flag even after uninstall
  • Could iOS flutter app logs be viewed while running in release mode – started after previous closed state
  • 6 Addictive Mobile Game Ideas Inspired by Flappy Bird’s Simplicity

Post Categories

  • Apps (20)
  • ChatGPT (19)
  • Choosing a Framework (38)
  • Flutter (206)
  • Graphical User Interface (13)
  • Marketing (114)
  • Software Development (270)
  • Spring (43)
  • StartUp (21)
  • Uncategorized (4)
  • Uncategorized (15)
  • Vaadin (14)