-
-
Notifications
You must be signed in to change notification settings - Fork 279
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
base: main
Are you sure you want to change the base?
Changes from all commits
5854f37
bde4121
1c443e8
ef33c49
d0598b1
712a2ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+87
to
+95
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| 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); | |
| } | |
| try { | |
| 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); | |
| } | |
| } | |
| } catch (e, stackTrace) { | |
| debugPrint( | |
| 'Error while migrating access token for key "$persistSessionKey": $e'); | |
| debugPrint(stackTrace.toString()); |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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' | ||||||||||
|
Comment on lines
+9
to
+10
|
||||||||||
| sdk: '>=3.4.0 <4.0.0' | |
| flutter: '>=3.22.0' | |
| sdk: '>=3.3.0 <4.0.0' | |
| flutter: '>=3.19.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new migration logic in _maybeMigrateAccessToken lacks test coverage. Since the existing tests for SharedPreferencesLocalStorage use setMockInitialValues which may not properly test the migration from legacy SharedPreferences to SharedPreferencesAsync, add tests to verify: 1) successful migration when legacy data exists, 2) no-op when legacy data doesn't exist, and 3) proper cleanup of legacy storage after migration.