Navaron Bracke

add comment for barcode reader cancel on web

@@ -148,7 +148,8 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -148,7 +148,8 @@ class MobileScannerWeb extends MobileScannerPlatform {
148 return; 148 return;
149 } 149 }
150 150
151 - _barcodesSubscription?.cancel(); 151 + // Ensure the barcode scanner is stopped, by cancelling the subscription.
  152 + await _barcodesSubscription?.cancel();
152 _barcodesSubscription = null; 153 _barcodesSubscription = null;
153 154
154 await _barcodeReader.stop(); 155 await _barcodeReader.stop();
@@ -160,6 +160,9 @@ final class ZXingBarcodeReader extends BarcodeReader { @@ -160,6 +160,9 @@ final class ZXingBarcodeReader extends BarcodeReader {
160 ); 160 );
161 }; 161 };
162 162
  163 + // The onCancel() method of the controller is called
  164 + // when the stream subscription returned by this method is cancelled in `MobileScannerWeb.stop()`.
  165 + // This avoids both leaving the barcode scanner running and a memory leak for the stream subscription.
163 controller.onCancel = () async { 166 controller.onCancel = () async {
164 _reader?.stopContinuousDecode.callAsFunction(); 167 _reader?.stopContinuousDecode.callAsFunction();
165 _reader?.reset.callAsFunction(); 168 _reader?.reset.callAsFunction();