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.


static Future<SharedPreferences> getInstance() async {
  if (_completer == null) {
    final Completer<SharedPreferences> completer =
    try {
      final Map<String, Object> preferencesMap =
          await _getSharedPreferencesMap();
    } 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.
      final Future<SharedPreferences> sharedPrefsFuture = completer.future;
      _completer = null;
      return sharedPrefsFuture;
    _completer = completer;
  return _completer!.future;