Init preferences once
This commit is contained in:
parent
85230d8df4
commit
35f7029e12
1 changed files with 6 additions and 0 deletions
|
|
@ -7,6 +7,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:shared_preferences_android/shared_preferences_android.dart';
|
import 'package:shared_preferences_android/shared_preferences_android.dart';
|
||||||
|
|
||||||
class Preferences {
|
class Preferences {
|
||||||
|
static Future<void>? _initFuture;
|
||||||
static late SharedPreferencesWithCache instance;
|
static late SharedPreferencesWithCache instance;
|
||||||
|
|
||||||
static const String id = 'id';
|
static const String id = 'id';
|
||||||
|
|
@ -27,6 +28,11 @@ class Preferences {
|
||||||
static const String lastHeading = 'lastHeading';
|
static const String lastHeading = 'lastHeading';
|
||||||
|
|
||||||
static Future<void> init() async {
|
static Future<void> init() async {
|
||||||
|
_initFuture ??= _createInstance();
|
||||||
|
await _initFuture;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<void> _createInstance() async {
|
||||||
instance = await SharedPreferencesWithCache.create(
|
instance = await SharedPreferencesWithCache.create(
|
||||||
sharedPreferencesOptions: Platform.isAndroid
|
sharedPreferencesOptions: Platform.isAndroid
|
||||||
? SharedPreferencesAsyncAndroidOptions(backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences)
|
? SharedPreferencesAsyncAndroidOptions(backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue