
Wraps platform-specific persistent storage for simple data. Supported data types are String
, int
, double
and bool
.
Platform |
Storage |
Keystore |
Android |
DataStore Preferences |
Android Keystore |
iOS |
NSUserDefaults |
Keychain |
macOS |
NSUserDefaults |
Keychain |
Web |
localStorage |
NOT SUPPORTED |
final EnhancedPreferences prefs = EnhancedPreferences();
// Specify options.
final EnhancedPreferencesOptions options = EnhancedPreferencesOptions();
// Save an String value to 'hello' key.
await prefs.setString('hello', 'World', options);
// Save an integer value to 'counter' key.
await prefs.setInt('counter', 10, options);
// Save an double value to 'rate' key.
await prefs.setDouble('rate', 0.9, options);
// Save an boolean value to 'isActive' key.
await prefs.setBool('isActive', true, options);
final EnhancedPreferences prefs = EnhancedPreferences();
// Specify options.
final EnhancedPreferencesOptions options = EnhancedPreferencesOptions();
// Try reading data from the 'Hello' key.
final String? hello = await prefs.getString('hello', options);
// Try reading data from the 'counter' key.
final int? counter = await prefs.getInt('counter', options);
// Try reading data from the 'rate' key.
final double? rate = await prefs.getDouble('rate', options);
// Try reading data from the 'isActive' key.
final bool? isActive = await prefs.getBool('isActive', options);
// Remove data for the 'hello' key.
await prefs.remove('hello');
// Get all keys.
final List<String>? keys = await prefs.keys();
Option |
Type |
Detail |
Default |
enableCache |
bool |
Whether to enable cache. |
true |
enableEncryption |
bool |
Whether to enable encryption. It is not supported on the web platform because sensitive data must be stored on the server for security reasons and it is unable to manage keys safely in browsers. |
false |
Error code |
Detail |
INVALID_ARGUMENT |
Invalid arguments such as the key is blank. |
REFERENCE_ERROR |
Failed to obtain the value for key. |
ILLEGAL_ACCESS |
Failed to encrypt / decrypt the value for key. |
UNKNOWN_ERROR |
Other causes. |
