Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Cancelling Stream Subscriptions | Streams Basics
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Dart Asynchronous Flow

bookCancelling 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

main.dart

copy
1234567891011121314151617181920
void 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.'); }); }
question mark

Which statement about managing and cancelling Stream subscriptions in Dart is correct?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

bookCancelling 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

main.dart

copy
1234567891011121314151617181920
void 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.'); }); }
question mark

Which statement about managing and cancelling Stream subscriptions in Dart is correct?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 3
some-alt