Cancelling Stream Subscriptions
When working with streams in Dart, you often need to control how and when you receive events. The StreamSubscription object is what you get when you start listening to a stream. It acts as a handle to your subscription, allowing you to pause, resume, or cancel the flow of events. Managing your StreamSubscription is crucial, especially in scenarios where you want to stop processing data—such as when a user navigates away from a page, when you only care about the first few events, or when you want to free up resources.
Cancelling a stream subscription is important because it stops the delivery of further events and closes the underlying resources. If you don't cancel subscriptions that are no longer needed, your app may continue to process unnecessary data, leading to memory leaks or unexpected behavior.
To cancel a subscription, you call the cancel() method on the StreamSubscription object. This is an asynchronous operation, so you can await it if you need to know when the cancellation is complete.
main.dart
1234567891011121314151617181920void main() async { // Create a stream that emits numbers from 1 to 5, one every 300ms. Stream<int> numberStream = Stream.periodic(Duration(milliseconds: 300), (count) => count + 1) .take(5); // Listen to the stream and keep the subscription so we can cancel it. StreamSubscription<int>? subscription; subscription = numberStream.listen((value) { print('Received: $value'); // Cancel after receiving the value 3. if (value == 3) { print('Cancelling subscription.'); subscription?.cancel(); } }, onDone: () { print('Stream is done.'); }); }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Fantastico!
Completion tasso migliorato a 9.09
Cancelling Stream Subscriptions
Scorri per mostrare il menu
When working with streams in Dart, you often need to control how and when you receive events. The StreamSubscription object is what you get when you start listening to a stream. It acts as a handle to your subscription, allowing you to pause, resume, or cancel the flow of events. Managing your StreamSubscription is crucial, especially in scenarios where you want to stop processing data—such as when a user navigates away from a page, when you only care about the first few events, or when you want to free up resources.
Cancelling a stream subscription is important because it stops the delivery of further events and closes the underlying resources. If you don't cancel subscriptions that are no longer needed, your app may continue to process unnecessary data, leading to memory leaks or unexpected behavior.
To cancel a subscription, you call the cancel() method on the StreamSubscription object. This is an asynchronous operation, so you can await it if you need to know when the cancellation is complete.
main.dart
1234567891011121314151617181920void main() async { // Create a stream that emits numbers from 1 to 5, one every 300ms. Stream<int> numberStream = Stream.periodic(Duration(milliseconds: 300), (count) => count + 1) .take(5); // Listen to the stream and keep the subscription so we can cancel it. StreamSubscription<int>? subscription; subscription = numberStream.listen((value) { print('Received: $value'); // Cancel after receiving the value 3. if (value == 3) { print('Cancelling subscription.'); subscription?.cancel(); } }, onDone: () { print('Stream is done.'); }); }
Grazie per i tuoi commenti!