Migrate compass_app to SharedPreferencesAsync

pull/2841/head
Harsh Yadav 3 weeks ago
parent 56bf76f2f0
commit a9eb5b2906

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

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

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

Loading…
Cancel
Save