-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathshared_preferences.dart
34 lines (28 loc) · 1.43 KB
/
shared_preferences.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences/util/legacy_to_async_migration_util.dart';
/// A helper class that manages preferences using SharedPreferencesAsync
/// and handles migration from the legacy SharedPreferences to
/// SharedPreferencesAsync.
class PreferenceHelper {
SharedPreferencesAsync _asyncPref = SharedPreferencesAsync();
PreferenceHelper._instantiate();
static final PreferenceHelper _instance = PreferenceHelper._instantiate();
static SharedPreferencesAsync get asyncPref => _instance._asyncPref;
static PreferenceHelper get instance => _instance;
/// Migration function that ensures any legacy data stored in
/// SharedPreferences is migrated to SharedPreferencesAsync. This migration
/// only happens once, as checked by the migrationCompletedKey.
///
/// [migrationCompletedKey] is used to track if the migration has been
/// completed.
Future<void> migrationSupportFunctionForSharedPreferences() async {
const SharedPreferencesOptions sharedPreferencesOptions = SharedPreferencesOptions();
final SharedPreferences prefs = await SharedPreferences.getInstance();
await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary(
legacySharedPreferencesInstance: prefs,
sharedPreferencesAsyncOptions: sharedPreferencesOptions,
migrationCompletedKey: 'migrationCompleted',
);
_asyncPref = SharedPreferencesAsync();
}
}