fix: overgo issues with flutter_secure_store

This commit is contained in:
Sorunome 2020-10-07 18:39:21 +02:00
parent 999f21ffbd
commit 6d0f344b67
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C
2 changed files with 34 additions and 2 deletions

View file

@ -175,7 +175,7 @@ class ThemeSwitcherWidgetState extends State<ThemeSwitcherWidget> {
BuildContext context;
Future loadSelection(MatrixState matrix) async {
String item = await matrix.store.getItem('theme') ?? 'light';
String item = await matrix.store.getItem('theme') ?? 'system';
selectedTheme = Themes.values.firstWhere(
(e) => e.toString() == 'Themes.' + item,
orElse: () => Themes.system);

View file

@ -199,9 +199,30 @@ Future<void> migrate(String clientName, Database db, Store store) async {
});
}
// see https://github.com/mogol/flutter_secure_storage/issues/161#issuecomment-704578453
class AsyncMutex {
Completer<void> _completer;
Future<void> lock() async {
while (_completer != null) {
await _completer.future;
}
_completer = Completer<void>();
}
void unlock() {
assert(_completer != null);
final completer = _completer;
_completer = null;
completer.complete();
}
}
class Store {
final LocalStorage storage;
final FlutterSecureStorage secureStorage;
static final _mutex = AsyncMutex();
Store()
: storage = LocalStorage('LocalStorage'),
@ -217,9 +238,12 @@ class Store {
}
}
try {
await _mutex.lock();
return await secureStorage.read(key: key);
} catch (_) {
return null;
} finally {
_mutex.unlock();
}
}
@ -231,7 +255,12 @@ class Store {
if (value == null) {
return await secureStorage.delete(key: key);
} else {
try {
await _mutex.lock();
return await secureStorage.write(key: key, value: value);
} finally {
_mutex.unlock();
}
}
}
@ -245,9 +274,12 @@ class Store {
}
}
try {
await _mutex.lock();
return await secureStorage.readAll();
} catch (_) {
return {};
} finally {
_mutex.unlock();
}
}
}