Watch
1
0
Fork
You've already forked nexus
0
forked from Nexus/nexus

use SharedPreferencesAsync to avoid race condition in Sharedprefs

This was so painful to debug...
This commit is contained in:
Henry Hiles 2026-09-20 15:45:32 -04:00
commit e8fb3a8523
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
10 changed files with 69 additions and 44 deletions

View file

@ -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,
);
}