getInstance method Null safety

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>();
    try {
      final Map<String, Object> preferencesMap =
          await _getSharedPreferencesMap();
      completer.complete(SharedPreferences._(preferencesMap));
    } on Exception 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;
    }
    _completer = completer;
  }
  return _completer!.future;
}