fix: overgo issues with flutter_secure_store
This commit is contained in:
parent
999f21ffbd
commit
6d0f344b67
|
@ -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);
|
||||
|
|
|
@ -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 {
|
||||
return await secureStorage.write(key: key, value: value);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue