Skip to content

Commit

Permalink
fix: Fixed various problems with shared preferences and added a way t…
Browse files Browse the repository at this point in the history
…o migrate legacy `SharedPreferences` keys to `SharedPreferencesWithCache`.

See flutter/flutter#150732.
  • Loading branch information
Skyost committed Jan 11, 2025
1 parent caa65b1 commit 423f4a7
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 100 deletions.
43 changes: 18 additions & 25 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,6 @@ class _RouteWidget extends ConsumerStatefulWidget {

/// The route widget state.
class _RouteWidgetState extends ConsumerState<_RouteWidget> {
/// The [RateMyApp] instance.
RateMyApp? rateMyApp;

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -269,31 +266,27 @@ class _RouteWidgetState extends ConsumerState<_RouteWidget> {
fireImmediately: true,
);
}
if (widget.rateMyApp) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
RateMyApp rateMyApp = RateMyApp.customConditions(
appStoreIdentifier: Stores.appStoreIdentifier,
googlePlayIdentifier: Stores.googlePlayIdentifier,
conditions: [
SupportedPlatformsCondition(),
],
)..populateWithDefaultConditions();
await rateMyApp.init();
if (rateMyApp.shouldOpenDialog && mounted) {
rateMyApp.showRateDialog(context);
}
});
}
}

@override
Widget build(BuildContext context) {
Widget child = UnlockChallengeWidget(
child: widget.child,
);
return widget.rateMyApp
? RateMyAppBuilder(
onInitialized: (context, rateMyApp) {
if (rateMyApp.shouldOpenDialog) {
rateMyApp.showRateDialog(context);
}
},
rateMyApp: RateMyApp.customConditions(
appStoreIdentifier: Stores.appStoreIdentifier,
googlePlayIdentifier: Stores.googlePlayIdentifier,
conditions: [
SupportedPlatformsCondition(),
],
)..populateWithDefaultConditions(),
builder: (context) => child,
)
: child;
}
Widget build(BuildContext context) => UnlockChallengeWidget(
child: widget.child,
);

/// Handles a login link.
Future<void> handleLoginLink(Uri loginLink) async {
Expand Down
48 changes: 41 additions & 7 deletions lib/utils/shared_preferences_with_prefix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,47 @@ class SharedPreferencesWithPrefix {
/// Creates a new instance with the given options and reloads the cache from the platform data.
static Future<SharedPreferencesWithPrefix> create({
String prefix = kDebugMode ? 'flutter_debug.' : 'flutter.',
}) async =>
SharedPreferencesWithPrefix._(
sharedPreferences: await SharedPreferencesWithCache.create(
cacheOptions: SharedPreferencesWithCacheOptions(),
),
prefix: prefix,
);
}) async {
SharedPreferencesWithPrefix sharedPreferencesWithPrefix = SharedPreferencesWithPrefix._(
sharedPreferences: await SharedPreferencesWithCache.create(
cacheOptions: SharedPreferencesWithCacheOptions(),
),
prefix: prefix,
);
await _migrate(sharedPreferencesWithPrefix);
return sharedPreferencesWithPrefix;
}

/// Migrates from [SharedPreferences] to [SharedPreferencesWithCache].
/// See https://github.com/flutter/flutter/issues/150732.
static Future<void> _migrate(SharedPreferencesWithPrefix sharedPreferencesWithPrefix) async {
SharedPreferences legacyPreferences = await SharedPreferences.getInstance();
bool hasMigrated = legacyPreferences.getBool('legacyPreferencesDidMigrate') == true;
if (hasMigrated) {
return;
}
Set<String> keys = legacyPreferences.getKeys();
for (String key in keys) {
Object? value = legacyPreferences.get(key);
bool canMigrate = value is String || value is bool || value is int || value is double || value is List<String>;
if (!canMigrate) {
continue;
}
await legacyPreferences.remove(key);
if (value is String) {
await sharedPreferencesWithPrefix.setString(key, value);
} else if (value is bool) {
await sharedPreferencesWithPrefix.setBool(key, value);
} else if (value is int) {
await sharedPreferencesWithPrefix.setInt(key, value);
} else if (value is double) {
await sharedPreferencesWithPrefix.setDouble(key, value);
} else if (value is List<String>) {
await sharedPreferencesWithPrefix.setStringList(key, value);
}
}
legacyPreferences.setBool('legacyPreferencesDidMigrate', true);
}

/// Returns true if cache contains the given [key].
///
Expand Down
Loading

0 comments on commit 423f4a7

Please sign in to comment.