watchSecrets method
Returns a Stream that emits fresh secrets on interval (default: 5 min).
The first value is emitted immediately. On each successful fetch the result is written to the SecretCache (if configured). Cancel the subscription to stop polling.
Failures are delivered, never swallowed (#1162). When a poll fails,
the last known good value (in memory, or the cache seed) is re-emitted as
before — so the stream never closes on connectivity loss — and then the
failure itself is delivered as an error event. Every failure is
delivered: a lost connection or timeout (DioException), an expired or
revoked credential (the API's 401/403), and an E2EEResponseError when
the server answered in plaintext although this client presented its E2EE
key, or the envelope would not decrypt (unwrapped from Dio, so its code
is e2ee-plaintext-response / e2ee-decryption-failed).
The error comes AFTER the re-emitted value, so it is the latest event: a
listener that clears its error state in onData (or a StreamBuilder)
still shows the failure until a later poll succeeds. Polling continues
after an error, but a listener subscribed with cancelOnError: true is
cancelled by the first one — pass an onError handler instead.
client.watchSecrets().listen((secrets) {
setState(() => _dbUrl = secrets['DATABASE_URL']);
}, onError: (Object e) {
setState(() => _error = e is E2EEResponseError ? e.code : '$e');
});
Implementation
Stream<Map<String, String>> watchSecrets({
Duration interval = const Duration(minutes: 5),
String? projectRef,
String? environmentSlug,
}) async* {
// Seed from cache so first emission is instant even when offline.
Map<String, String> last = await _tryReadCache() ?? {};
while (true) {
Object? failure;
StackTrace? failureTrace;
try {
last = await pullSecrets(
projectRef: projectRef,
environmentSlug: environmentSlug,
fallbackOnError: false, // cache write-through already done inside pullSecrets
);
} catch (e, st) {
// #1050 — surface a refused E2EE answer by itself, with its code, not Dio's wrapper around it.
failure = e is DioException && e.error is E2EEResponseError ? e.error : e;
failureTrace = st;
}
// Re-emit last known good (in-memory or from the cache seed above) on failure, as documented.
yield last;
if (failure != null) {
// An error event does not close an async* stream: polling carries on after it.
yield* Stream<Map<String, String>>.error(failure, failureTrace);
}
await Future<void>.delayed(interval);
}
}