Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

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

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 3
some-alt