StreamShard<T> manages state from a Stream. Perfect for real-time data like WebSocket connections, database listeners, or timers.
See Async Values for the AsyncValue<T> sealed class and Widgets for AsyncShardBuilder.
class MessagesStream extends StreamShard<List<Message>> {
final String chatId;
final ChatRepository repository;
MessagesStream({required this.chatId, required this.repository});
@override
Stream<List<Message>> build() {
return repository.watchMessages(chatId);
}
}
Call refresh() to cancel current subscription and re-subscribe:
// Reset the stream
context.read<TimerShard>().refresh();
Use pause() and resume() to temporarily stop processing stream events without cancelling the subscription:
final messages = context.read<MessagesStream>();
messages.pause(); // pause the underlying subscription
messages.isPaused; // true
messages.resume(); // deliver buffered events and continue
While paused, source events are buffered per Dart's StreamSubscription.pause semantics and delivered on resume. It's a no-op if the shard hasn't subscribed yet (for example, before onInit). Use it to stop processing while a screen is backgrounded.
Next: Persistent Shard to persist state across app sessions. See also: Examples.