Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Cancelling Stream Subscriptions | Streams Basics
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

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show an example of how to cancel a StreamSubscription in Dart?

What happens if I forget to cancel a StreamSubscription?

Are there best practices for managing multiple StreamSubscriptions?

bookCancelling Stream Subscriptions

Swipe to show 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

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 3
some-alt