build method
Initialize a Notifier.
It is safe to use Ref.watch or Ref.listen inside this method.
If a dependency of this Notifier (when using Ref.watch) changes,
then build will be re-executed. On the other hand, the Notifier
will not be recreated. Its instance will be preserved between
executions of build.
If this method throws, reading this provider will rethrow the error.
Implementation
@override
AsyncValue<T> build() {
final identity = ref.watch(walletIdentityProvider);
final target = read(identity);
// Legacy-refresh forwarding (see the invalidation contract above): a
// manual invalidate of THIS view refreshes the current reader. The
// registration is per-build, so it always carries the live target.
// Experimental member: riverpod's own doc example is exactly this
// forwarding shape; re-verify on upgrade.
// ignore: experimental_member_use
ref.onManualInvalidation(() => ref.invalidate(target));
// Listen BEFORE the seed read: the subscription is what keeps the
// autoDispose reader element alive from its very first frame.
ref.listen(target, (_, next) {
scheduleMicrotask(() {
// The notifier may have been torn down — or re-keyed to a NEW
// identity — while the write waited. `ref.mounted` covers teardown;
// the identity re-check covers a re-key racing the deferral (the
// stale identity's last transition must never overwrite the new
// seed). A fast flip-back to the SAME session object passes the
// re-check by design: a session-keyed element only ever carries its
// OWN wallet's data, and the flip-back forces a fresh element
// anyway (the old one disposed at the flip — probe-pinned).
if (!ref.mounted) return;
if (ref.read(walletIdentityProvider) != identity) return;
state = next;
});
});
return ref.read(target);
}