forked from Nexus/nexus
use SharedPreferencesAsync to avoid race condition in Sharedprefs
This was so painful to debug...
This commit is contained in:
parent
0d1ace3fd0
commit
e8fb3a8523
10 changed files with 69 additions and 44 deletions
|
|
@ -4,18 +4,19 @@ import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
|||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/controllers/key.dart";
|
||||
|
||||
class PushKeyController(final String instance) extends Notifier<String?> {
|
||||
class PushKeyController(final String instance) extends AsyncNotifier<String?> {
|
||||
@override
|
||||
String? build() => json.decode(
|
||||
ref.watch(KeyController.provider(KeyController.pushKeyKey)) ?? "{}",
|
||||
Future<String?> build() async => json.decode(
|
||||
await ref.watch(KeyController.provider(KeyController.pushKeyKey).future) ??
|
||||
"{}",
|
||||
)[instance];
|
||||
|
||||
Future<void> set(String? value) async {
|
||||
final provider = KeyController.provider(KeyController.pushKeyKey);
|
||||
final notifier = ref.watch(provider.notifier);
|
||||
final prefs = IMap(json.decode(ref.watch(provider) ?? "{}"));
|
||||
final prefs = IMap(json.decode(await ref.watch(provider.future) ?? "{}"));
|
||||
|
||||
state = value;
|
||||
state = .data(value);
|
||||
notifier.set(
|
||||
json.encode(
|
||||
prefs.add(instance, value).where((_, value) => value != null).unlock,
|
||||
|
|
@ -24,7 +25,7 @@ class PushKeyController(final String instance) extends Notifier<String?> {
|
|||
}
|
||||
|
||||
static final provider =
|
||||
NotifierProvider.family<PushKeyController, String?, String>(
|
||||
AsyncNotifierProvider.family<PushKeyController, String?, String>(
|
||||
PushKeyController.new,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue