简体   繁体   中英

Cancelling a StreamSubscription in flutter

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 ?

You have to await for the subscription cancelation so do:

await usbStream.cancel();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM