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:shared_preferences/shared_preferences.dart';
import 'package:shared_preferences_android/shared_preferences_android.dart';
class Preferences {
static late SharedPreferences instance;
static late SharedPreferencesWithCache instance;
static const String id = 'id';
static const String url = 'url';
static const String accuracy = 'accuracy';
@ -15,7 +16,18 @@ class Preferences {
static const String buffer = 'buffer';
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) {
await _migrate();
}
@ -113,12 +125,6 @@ class Preferences {
instance.setInt(distance, oldDistance);
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');
if (oldBuffer != null) {
instance.setBool(buffer, oldBuffer);