1
0
mirror of https://github.com/flutter/samples.git synced 2026-06-25 15:49:43 +00:00

Migrate compass_app to SharedPreferencesAsync (#2841)

## Description

Migrated `SharedPreferences` usage in `compass_app` to
`SharedPreferencesAsync` to align with the latest `shared_preferences`
recommendations.

### Changes made

* Replaced `SharedPreferences.getInstance()` usages
* Updated async preference reads with awaited getters
* Migrated token persistence logic
* Updated test setup preference clearing logic

Fixes #2720

## Pre-launch Checklist

* [x] I read the [[Flutter Style
Guide](https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md)]
*recently*, and have followed its advice.
* [x] I signed the [[CLA](https://cla.developers.google.com/)].
* [x] I read the [[Contributors
Guide](https://github.com/flutter/samples/blob/main/CONTRIBUTING.md)].
* [ ] I have added sample code updates to the
[[changelog](https://chatgpt.com/CHANGELOG.md)].
* [ ] I updated/added relevant documentation (doc comments with `///`).

<!-- Links -->

[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md
[CLA]: https://cla.developers.google.com/
[Discord]:
https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
[Contributors Guide]:
https://github.com/flutter/samples/blob/main/CONTRIBUTING.md
[changelog]: ../CHANGELOG.md

Co-authored-by: Eric Windmill <eric@ericwindmill.com>
This commit is contained in:
Harsh Yadav
2026-06-20 03:32:33 +05:30
committed by GitHub
parent d9a2b781a2
commit 7e12f75def
3 changed files with 16 additions and 8 deletions

View File

@@ -65,10 +65,12 @@ class _MyHomePageState extends State<MyHomePage> {
// Write the value to [SharedPreferences] which will get read on the // Write the value to [SharedPreferences] which will get read on the
// [SimpleDatabase]'s isolate. For this example the value is always true // [SimpleDatabase]'s isolate. For this example the value is always true
// just for demonstration purposes. // just for demonstration purposes.
final Future<void> sharedPreferencesSet = SharedPreferences.getInstance() final SharedPreferencesAsync sharedPreferences = SharedPreferencesAsync();
.then(
(sharedPreferences) => sharedPreferences.setBool('isDebug', true), final Future<void> sharedPreferencesSet = sharedPreferences.setBool(
); 'isDebug',
true,
);
final Future<Directory> tempDirFuture = path_provider final Future<Directory> tempDirFuture = path_provider
.getTemporaryDirectory(); .getTemporaryDirectory();

View File

@@ -30,7 +30,7 @@ void main() {
setUpAll(() async { setUpAll(() async {
// Clear any stored shared preferences // Clear any stored shared preferences
final sharedPreferences = await SharedPreferences.getInstance(); final sharedPreferences = SharedPreferencesAsync();
await sharedPreferences.clear(); await sharedPreferences.clear();
// Start the dart server // Start the dart server

View File

@@ -13,9 +13,11 @@ class SharedPreferencesService {
Future<Result<String?>> fetchToken() async { Future<Result<String?>> fetchToken() async {
try { try {
final sharedPreferences = await SharedPreferences.getInstance(); final sharedPreferences = SharedPreferencesAsync();
_log.finer('Got token from SharedPreferences'); _log.finer('Got token from SharedPreferences');
return Result.ok(sharedPreferences.getString(_tokenKey));
return Result.ok(await sharedPreferences.getString(_tokenKey));
} on Exception catch (e) { } on Exception catch (e) {
_log.warning('Failed to get token', e); _log.warning('Failed to get token', e);
return Result.error(e); return Result.error(e);
@@ -24,14 +26,18 @@ class SharedPreferencesService {
Future<Result<void>> saveToken(String? token) async { Future<Result<void>> saveToken(String? token) async {
try { try {
final sharedPreferences = await SharedPreferences.getInstance(); final sharedPreferences = SharedPreferencesAsync();
if (token == null) { if (token == null) {
_log.finer('Removed token'); _log.finer('Removed token');
await sharedPreferences.remove(_tokenKey); await sharedPreferences.remove(_tokenKey);
} else { } else {
_log.finer('Replaced token'); _log.finer('Replaced token');
await sharedPreferences.setString(_tokenKey, token); await sharedPreferences.setString(_tokenKey, token);
} }
return const Result.ok(null); return const Result.ok(null);
} on Exception catch (e) { } on Exception catch (e) {
_log.warning('Failed to set token', e); _log.warning('Failed to set token', e);