Skip to content

Commit 6a79849

Browse files
committed
fix: handle decode error of shared preferences
1 parent 2f4fc59 commit 6a79849

1 file changed

Lines changed: 27 additions & 8 deletions

File tree

lib/main.dart

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import 'dart:async';
22
import 'dart:convert';
3+
import 'dart:io';
34
import 'dart:ui';
45

56
import 'package:async/async.dart';
67
import 'package:collection/collection.dart';
7-
import 'package:file/file.dart';
88
import 'package:flutter/foundation.dart';
99
import 'package:flutter/material.dart';
1010
import 'package:flutter/services.dart';
@@ -14,6 +14,8 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
1414
import 'package:flutter_localizations/flutter_localizations.dart';
1515
import 'package:hooks_riverpod/hooks_riverpod.dart';
1616
import 'package:misskey_dart/misskey_dart.dart';
17+
import 'package:path/path.dart' as p;
18+
import 'package:path_provider/path_provider.dart';
1719
import 'package:shared_preferences/shared_preferences.dart';
1820
import 'package:shared_preferences/util/legacy_to_async_migration_util.dart';
1921
import 'package:twemoji_v2/twemoji_v2.dart';
@@ -72,9 +74,25 @@ void main() async {
7274
'misskey',
7375
], await rootBundle.loadString(Assets.misskey.license));
7476
});
75-
final prefs = await SharedPreferencesWithCache.create(
76-
cacheOptions: const SharedPreferencesWithCacheOptions(),
77-
);
77+
late final SharedPreferencesWithCache prefs;
78+
try {
79+
prefs = await SharedPreferencesWithCache.create(
80+
cacheOptions: const SharedPreferencesWithCacheOptions(),
81+
);
82+
} on FormatException {
83+
if (defaultTargetPlatform
84+
case TargetPlatform.linux || TargetPlatform.windows) {
85+
final directory = await getApplicationSupportDirectory();
86+
await File(
87+
p.join(directory.path, 'shared_preferences.json'),
88+
).rename(p.join(directory.path, '_shared_preferences.json'));
89+
prefs = await SharedPreferencesWithCache.create(
90+
cacheOptions: const SharedPreferencesWithCacheOptions(),
91+
);
92+
} else {
93+
rethrow;
94+
}
95+
}
7896
const migrationCompletedKey = 'migrationCompleted';
7997
if (!prefs.containsKey(migrationCompletedKey)) {
8098
await migrateLegacySharedPreferencesToSharedPreferencesAsyncIfNecessary(
@@ -668,13 +686,14 @@ class Aria extends HookConsumerWidget {
668686
body.fromUser?.avatarUrl,
669687
_ => null,
670688
};
671-
File? file;
689+
String? filePath;
672690
if (url != null && generalSettings.showImageInNotification) {
673691
try {
674-
file = await ref
692+
final file = await ref
675693
.read(cacheManagerProvider)
676694
.getSingleFile(url.toString())
677695
.timeout(const Duration(seconds: 10));
696+
filePath = file.path;
678697
} catch (_) {}
679698
}
680699

@@ -688,8 +707,8 @@ class Aria extends HookConsumerWidget {
688707
channel.name,
689708
styleInformation: BigTextStyleInformation(body ?? ''),
690709
color: ariaColor,
691-
largeIcon: file != null
692-
? FilePathAndroidBitmap(file.path)
710+
largeIcon: filePath != null
711+
? FilePathAndroidBitmap(filePath)
693712
: null,
694713
groupKey: account.toString(),
695714
subText: account.toString(),

0 commit comments

Comments
 (0)