Skip to content

Instantly share code, notes, and snippets.

@okiookio
Created July 30, 2022 07:12
Show Gist options
  • Save okiookio/7066b4ba5612198461e2290ba3dedad8 to your computer and use it in GitHub Desktop.
Save okiookio/7066b4ba5612198461e2290ba3dedad8 to your computer and use it in GitHub Desktop.
A Sample of Shared Preferences và Security Storage in real project.
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
/// Base class containing a unified API for key-value pairs' storage.
/// This class provides low level methods for storing:
/// - Sensitive keys using [FlutterSecureStorage]
/// - Insensitive keys using [SharedPreferences]
class DefaultKeyValueStorage {
/// Instance of shared preferences
static SharedPreferences? _sharedPrefs;
/// Instance of flutter secure storage
static FlutterSecureStorage? _secureStorage;
/// Singleton instance of KeyValueStorage Helper
static DefaultKeyValueStorage? _instance;
/// Get instance of this class
factory DefaultKeyValueStorage() =>
_instance ?? const DefaultKeyValueStorage._();
/// Private constructor
const DefaultKeyValueStorage._();
/// Initializer for shared prefs and flutter secure storage
/// Should be called in main before runApp and
/// after WidgetsBinding.FlutterInitialized(), to allow for synchronous tasks
/// when possible.
static Future<void> init() async {
_sharedPrefs ??= await SharedPreferences.getInstance();
_secureStorage ??= const FlutterSecureStorage();
}
/// Reads the value for the key from common preferences storage
T? getCommon<T>(String key) {
try {
switch (T) {
case String:
return _sharedPrefs!.getString(key) as T?;
case int:
return _sharedPrefs!.getInt(key) as T?;
case bool:
return _sharedPrefs!.getBool(key) as T?;
case double:
return _sharedPrefs!.getDouble(key) as T?;
default:
return _sharedPrefs!.get(key) as T?;
}
} on Exception catch (ex) {
debugPrint('$ex');
return null;
}
}
/// Reads the decrypted value for the key from secure storage
Future<String?> getEncrypted(String key) {
try {
return _secureStorage!.read(key: key);
} on PlatformException catch (ex) {
debugPrint('$ex');
return Future<String?>.value();
}
}
/// Sets the value for the key to common preferences storage
Future<bool> setCommon<T>(String key, T value) async {
switch (T) {
case String:
return await _sharedPrefs!.setString(key, value as String);
case int:
return await _sharedPrefs!.setInt(key, value as int);
case bool:
return _sharedPrefs!.setBool(key, value as bool);
case double:
return await _sharedPrefs!.setDouble(key, value as double);
default:
return await _sharedPrefs!.setString(key, value as String);
}
}
/// Sets the encrypted value for the key to secure storage
Future<bool> setEncrypted(String key, String value) {
try {
_secureStorage!.write(key: key, value: value);
return Future.value(true);
} on PlatformException catch (ex) {
debugPrint('$ex');
return Future.value(false);
}
}
/// Erases common preferences keys
Future<bool> clearCommon() => _sharedPrefs!.clear();
/// Erases encrypted keys
Future<bool> clearEncrypted() async {
try {
await _secureStorage!.deleteAll();
return true;
} on PlatformException catch (ex) {
debugPrint('$ex');
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment