Skip to content

fix: Adds support for SharedPreferencesAsync #1164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions packages/supabase_flutter/lib/src/local_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,32 @@ class EmptyLocalStorage extends LocalStorage {
/// A [LocalStorage] implementation that implements SharedPreferences as the
/// storage method.
class SharedPreferencesLocalStorage extends LocalStorage {
late final SharedPreferences _prefs;
late final SharedPreferences _syncPrefs;

late final SharedPreferencesAsync _asyncPrefs;

SharedPreferencesLocalStorage({required this.persistSessionKey});
SharedPreferencesLocalStorage({
required this.persistSessionKey,
this.useSharedPreferencesAsync = false,
});

final String persistSessionKey;

final bool useSharedPreferencesAsync;

static const _useWebLocalStorage =
kIsWeb && bool.fromEnvironment("dart.library.js_interop");

@override
Future<void> initialize() async {
if (!_useWebLocalStorage) {
WidgetsFlutterBinding.ensureInitialized();
_prefs = await SharedPreferences.getInstance();

if (useSharedPreferencesAsync) {
_asyncPrefs = SharedPreferencesAsync();
} else {
_syncPrefs = await SharedPreferences.getInstance();
}
}
}

Expand All @@ -85,23 +98,36 @@ class SharedPreferencesLocalStorage extends LocalStorage {
if (_useWebLocalStorage) {
return web.hasAccessToken(persistSessionKey);
}
return _prefs.containsKey(persistSessionKey);

return switch (useSharedPreferencesAsync) {
true => _asyncPrefs.containsKey(persistSessionKey),
false => Future.value(_syncPrefs.containsKey(persistSessionKey)),
};
}

@override
Future<String?> accessToken() async {
if (_useWebLocalStorage) {
return web.accessToken(persistSessionKey);
}
return _prefs.getString(persistSessionKey);

return switch (useSharedPreferencesAsync) {
true => _asyncPrefs.getString(persistSessionKey),
false => Future.value(_syncPrefs.getString(persistSessionKey)),
};
}

@override
Future<void> removePersistedSession() async {
if (_useWebLocalStorage) {
web.removePersistedSession(persistSessionKey);
} else {
await _prefs.remove(persistSessionKey);
switch (useSharedPreferencesAsync) {
case true:
await _asyncPrefs.remove(persistSessionKey);
case false:
await _syncPrefs.remove(persistSessionKey);
}
}
}

Expand All @@ -110,7 +136,10 @@ class SharedPreferencesLocalStorage extends LocalStorage {
if (_useWebLocalStorage) {
return web.persistSession(persistSessionKey, persistSessionString);
}
return _prefs.setString(persistSessionKey, persistSessionString);
return switch (useSharedPreferencesAsync) {
true => _asyncPrefs.setString(persistSessionKey, persistSessionString),
false => _syncPrefs.setString(persistSessionKey, persistSessionString),
};
}
}

Expand Down
Loading