Fix preference migration

This commit is contained in:
Anton Tananaev 2025-06-03 22:27:07 -07:00
parent 15100aaa9c
commit 1b020ab2e0
3 changed files with 16 additions and 9 deletions

View file

@ -4,9 +4,10 @@ import 'dart:math';
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg; import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_android/shared_preferences_android.dart';
class Preferences { class Preferences {
static late SharedPreferences instance; static late SharedPreferencesWithCache instance;
static const String id = 'id'; static const String id = 'id';
static const String url = 'url'; static const String url = 'url';
static const String accuracy = 'accuracy'; static const String accuracy = 'accuracy';
@ -15,7 +16,18 @@ class Preferences {
static const String buffer = 'buffer'; static const String buffer = 'buffer';
static Future<void> init() async { static Future<void> init() async {
instance = await SharedPreferences.getInstance(); instance = await SharedPreferencesWithCache.create(
sharedPreferencesOptions: Platform.isAndroid
? SharedPreferencesAsyncAndroidOptions(backend: SharedPreferencesAndroidBackendLibrary.SharedPreferences)
: SharedPreferencesOptions(),
cacheOptions: SharedPreferencesWithCacheOptions(
allowList: {
'id', 'url', 'accuracy', 'interval', 'distance', 'buffer',
'device_id_preference', 'server_url_preference', 'accuracy_preference',
'frequency_preference', 'distance_preference', 'buffer_preference',
},
),
);
if (Platform.isIOS) { if (Platform.isIOS) {
await _migrate(); await _migrate();
} }
@ -113,12 +125,6 @@ class Preferences {
instance.setInt(distance, oldDistance); instance.setInt(distance, oldDistance);
instance.remove('distance_preference'); instance.remove('distance_preference');
} }
final oldAngleString = instance.getString('angle_preference');
final oldAngle = oldAngleString != null ? int.tryParse(oldAngleString) : null;
if (oldAngle != null) {
instance.setInt('angle', oldAngle);
instance.remove('angle_preference');
}
final oldBuffer = instance.getBool('buffer_preference'); final oldBuffer = instance.getBool('buffer_preference');
if (oldBuffer != null) { if (oldBuffer != null) {
instance.setBool(buffer, oldBuffer); instance.setBool(buffer, oldBuffer);

View file

@ -374,7 +374,7 @@ packages:
source: hosted source: hosted
version: "2.5.3" version: "2.5.3"
shared_preferences_android: shared_preferences_android:
dependency: transitive dependency: "direct main"
description: description:
name: shared_preferences_android name: shared_preferences_android
sha256: "20cbd561f743a342c76c151d6ddb93a9ce6005751e7aa458baad3858bfbfb6ac" sha256: "20cbd561f743a342c76c151d6ddb93a9ce6005751e7aa458baad3858bfbfb6ac"

View file

@ -20,6 +20,7 @@ dependencies:
firebase_crashlytics: ^4.3.5 firebase_crashlytics: ^4.3.5
quick_actions: ^1.1.0 quick_actions: ^1.1.0
rate_my_app: ^2.3.1 rate_my_app: ^2.3.1
shared_preferences_android: ^2.4.10
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: