getInstance static method

Future<SharedPreferences> getInstance()

Loads and parses the SharedPreferences for this app from disk.

Because this is reading from disk, it shouldn't be awaited in performance-sensitive blocks.

Implementation

static Future<SharedPreferences> getInstance() async {
  if (_completer == null) {
    final Completer<SharedPreferences> completer =
        Completer<SharedPreferences>();
    _completer = completer;
    try {
      final Map<String, Object> preferencesMap =
          await _getSharedPreferencesMap();
      completer.complete(SharedPreferences._(preferencesMap));
    } catch (e) {
      // If there's an error, explicitly return the future with an error.
      // then set the completer to null so we can retry.
      completer.completeError(e);
      final Future<SharedPreferences> sharedPrefsFuture = completer.future;
      _completer = null;
      return sharedPrefsFuture;
    }
  }
  return _completer!.future;
}