Debug School

rakesh kumar
rakesh kumar

Posted on

Difference between shared preference and flutter secure storage in flutter

In Flutter, both SharedPreferences and flutter_secure_storage are used for storing data locally on the device, but they serve different purposes and have different security features. Here's a detailed comparison:

SharedPreferences

Purpose:

Used for storing simple key-value pairs of data.
Typically used for storing non-sensitive data such as user preferences, settings, and other small amounts of app state information.
Features:

  1. Data is stored in plain text.
  2. Suitable for storing simple data types like strings, integers, booleans, and lists.
  3. Easy to use and integrates well with Flutter apps.
  4. Data is stored in a shared preferences file that is not encrypted . Use Cases:

Saving user preferences (e.g., theme selection, language choice).
Storing simple state information (e.g., whether a user has seen an introductory tutorial).
Example:

import 'package:shared_preferences/shared_preferences.dart';

Future<void> saveData() async {
  final prefs = await SharedPreferences.getInstance();
  await prefs.setString('key', 'value');
}

Future<void> loadData() async {
  final prefs = await SharedPreferences.getInstance();
  final value = prefs.getString('key');
  print(value);
}
Enter fullscreen mode Exit fullscreen mode

flutter_secure_storage

Purpose:

Used for storing sensitive data securely.
Typically used for storing credentials, tokens, and other sensitive information that needs to be kept secure.
Features:

  1. Data is encrypted and stored securely.
  2. Uses platform-specific secure storage mechanisms (Keychain on iOS, Keystore on Android).
  3. Provides an additional layer of security to protect sensitive information . Use Cases:

Storing authentication tokens (e.g., OAuth tokens, JWTs).
Storing sensitive user information (e.g., passwords, encryption keys).
Example:

import 'package:flutter_secure_storage/flutter_secure_storage.dart';

final secureStorage = FlutterSecureStorage();

Future<void> saveSecureData() async {
  await secureStorage.write(key: 'key', value: 'value');
}

Future<void> loadSecureData() async {
  final value = await secureStorage.read(key: 'key');
  print(value);
}
Enter fullscreen mode Exit fullscreen mode

Key Differences

Security: SharedPreferences stores data in plain text, while flutter_secure_storage encrypts the data, providing a higher level of security.
Use Case: Use SharedPreferences for non-sensitive data that doesn't require encryption. Use flutter_secure_storage for sensitive data that needs to be protected.
Platform Integration: flutter_secure_storage leverages platform-specific secure storage solutions (Keychain, Keystore) to ensure data is stored securely.

Summary

SharedPreferences: Best for storing non-sensitive, simple key-value data.
flutter_secure_storage: Best for storing sensitive data securely with encryption.
Choosing between these two depends on the nature of the data you need to store. For sensitive information, always opt for flutter_secure_storage to ensure the security and privacy of the data. For less sensitive data, SharedPreferences is a simpler and faster option.

Top comments (0)