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 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 19 additions & 3 deletions packages/supabase_flutter/lib/src/local_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,38 @@ class EmptyLocalStorage extends LocalStorage {
Future<void> persistSession(persistSessionString) async {}
}

/// A [LocalStorage] implementation that implements SharedPreferences as the
/// A [LocalStorage] implementation that implements SharedPreferencesAsync as the
/// storage method.
class SharedPreferencesLocalStorage extends LocalStorage {
late final SharedPreferences _prefs;
late final SharedPreferencesAsync _prefs;

SharedPreferencesLocalStorage({required this.persistSessionKey});

final String persistSessionKey;

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

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

await _maybeMigrateAccessToken();
}
}

Future<void> _maybeMigrateAccessToken() async {
final legacyPrefs = await SharedPreferences.getInstance();

if (legacyPrefs.containsKey(persistSessionKey)) {
final accessToken = legacyPrefs.getString(persistSessionKey);

if (accessToken != null) {
await legacyPrefs.remove(persistSessionKey);
await _prefs.setString(persistSessionKey, accessToken);
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/supabase_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ repository: 'https://github.com/supabase/supabase-flutter/tree/main/packages/sup
documentation: 'https://supabase.com/docs/reference/dart/introduction'

environment:
sdk: '>=3.3.0 <4.0.0'
flutter: '>=3.19.0'
sdk: '>=3.4.0 <4.0.0'
flutter: '>=3.22.0'

dependencies:
app_links: '>=3.5.0 <7.0.0'
Expand All @@ -20,7 +20,7 @@ dependencies:
supabase: 2.7.0
url_launcher: ^6.1.2
path_provider: ^2.0.0
shared_preferences: ^2.0.0
shared_preferences: ^2.3.0
logging: ^1.2.0
web: '>=0.5.0 <2.0.0'

Expand Down
Loading