watchSecrets method

Stream<Map<String, String>> watchSecrets({
  1. Duration interval = const Duration(minutes: 5),
  2. String? projectRef,
  3. String? environmentSlug,
})

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). If a fetch fails the last known good value is re-emitted — the stream never closes due to connectivity loss. Cancel the subscription to stop polling.

client.watchSecrets().listen((secrets) {
  setState(() => _dbUrl = secrets['DATABASE_URL']);
});

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) {
    try {
      last = await pullSecrets(
        projectRef: projectRef,
        environmentSlug: environmentSlug,
        fallbackOnError: false, // cache write-through already done inside pullSecrets
      );
    } catch (_) {
      // Re-emit last known good (in-memory or from cache seed above).
    }
    yield last;
    await Future<void>.delayed(interval);
  }
}