-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix preferences initialization error
- Loading branch information
1 parent
249955e
commit 6b67ae6
Showing
2 changed files
with
38 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,33 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:loggy/loggy.dart'; | ||
import 'package:path/path.dart' as p; | ||
import 'package:path_provider/path_provider.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
part 'preferences_provider.g.dart'; | ||
|
||
@Riverpod(keepAlive: true) | ||
Future<SharedPreferences> sharedPreferences(SharedPreferencesRef ref) async => | ||
SharedPreferences.getInstance(); | ||
Future<SharedPreferences> sharedPreferences(SharedPreferencesRef ref) async { | ||
final logger = Loggy("preferences"); | ||
SharedPreferences? sharedPreferences; | ||
|
||
logger.debug("initializing preferences"); | ||
try { | ||
sharedPreferences = await SharedPreferences.getInstance(); | ||
} catch (e) { | ||
logger.error("error initializing preferences", e); | ||
if (!Platform.isWindows && !Platform.isLinux) { | ||
rethrow; | ||
} | ||
// https://github.com/flutter/flutter/issues/89211 | ||
final directory = await getApplicationSupportDirectory(); | ||
final file = File(p.join(directory.path, 'shared_preferences.json')); | ||
if (file.existsSync()) { | ||
file.deleteSync(); | ||
} | ||
} | ||
|
||
return sharedPreferences ??= await SharedPreferences.getInstance(); | ||
} |