Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancelling a StreamSubscription in flutter

Tags:

flutter

dart

I have the following stream subscription

Stream<Uint8List> stream = await USBSerialSingleton.instance.inputStream;
usbStream = stream.listen(onDataReceivedFromUSBSerial);

The inputStream is a broadcast stream exposed like this.

Future<Stream<Uint8List>> get inputStream async {
final UsbPort port = await this.port;
return port.inputStream.asBroadcastStream();
}

I would like to stop listening to the stream, so I am calling

usbStream.cancel();

But I keep receiving messages on my onDataReceivedFromUSBSerial method. I keep getting messages on the onDataReceivedFromUSBSerial even if I close the dialogue that all this is implemented on.

Question: How do I stop listening to my usbStream?

like image 436
DrkStr Avatar asked Mar 15 '26 09:03

DrkStr


1 Answers

You have to await for the subscription cancelation so do:

await usbStream.cancel();
like image 57
Michel Feinstein Avatar answered Mar 17 '26 03:03

Michel Feinstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!