useStreamSubscription<S> method

StreamSubscription<S> useStreamSubscription<S>({
  1. required String key,
  2. required Stream<S> stream,
  3. required void onData(
    1. S event
    ),
})

Subscribes to a stream and manages the subscription lifecycle.

key: A unique string key to identify the StreamSubscription. stream: The stream to subscribe to. onData: A callback to handle incoming data events.

Returns a StreamSubscription associated with the given key.

Implementation

StreamSubscription<S> useStreamSubscription<S>({
  required String key,
  required Stream<S> stream,
  required void Function(S event) onData,
}) {
  return _getOrCreate(
      key: key,
      create: () => stream.listen(onData),
      disposeHandler: (subscription) async => subscription.cancel());
}