@@ -18,14 +18,24 @@ class SharedPreferences {
1818 SharedPreferences ._(this ._preferenceCache);
1919
2020 static const String _prefix = 'flutter.' ;
21- static SharedPreferences _instance ;
21+ static Completer < SharedPreferences > _completer ;
2222 static Future <SharedPreferences > getInstance () async {
23- if (_instance == null ) {
24- final Map <String , Object > preferencesMap =
25- await _getSharedPreferencesMap ();
26- _instance = SharedPreferences ._(preferencesMap);
23+ if (_completer == null ) {
24+ _completer = Completer <SharedPreferences >();
25+ try {
26+ final Map <String , Object > preferencesMap =
27+ await _getSharedPreferencesMap ();
28+ _completer.complete (SharedPreferences ._(preferencesMap));
29+ } on Exception catch (e) {
30+ // If there's an error, explicitly return the future with an error.
31+ // then set the completer to null so we can retry.
32+ _completer.completeError (e);
33+ final Future <SharedPreferences > sharedPrefsFuture = _completer.future;
34+ _completer = null ;
35+ return sharedPrefsFuture;
36+ }
2737 }
28- return _instance ;
38+ return _completer.future ;
2939 }
3040
3141 /// The cache that holds all preferences.
@@ -168,7 +178,7 @@ class SharedPreferences {
168178
169179 /// Initializes the shared preferences with mock values for testing.
170180 ///
171- /// If the singleton instance has been initialized already, it is automatically reloaded .
181+ /// If the singleton instance has been initialized already, it is nullified .
172182 @visibleForTesting
173183 static void setMockInitialValues (Map <String , dynamic > values) {
174184 _kChannel.setMockMethodCallHandler ((MethodCall methodCall) async {
@@ -177,6 +187,6 @@ class SharedPreferences {
177187 }
178188 return null ;
179189 });
180- _instance ? . reload () ;
190+ _completer = null ;
181191 }
182192}
0 commit comments