Navaron Bracke

add helper to check disposal

@@ -94,6 +94,28 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { @@ -94,6 +94,28 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
94 /// Get the stream of scanned barcodes. 94 /// Get the stream of scanned barcodes.
95 Stream<BarcodeCapture> get barcodes => _barcodesController.stream; 95 Stream<BarcodeCapture> get barcodes => _barcodesController.stream;
96 96
  97 + bool _isDisposed = false;
  98 +
  99 + void _throwIfNotInitialized() {
  100 + if (!value.isInitialized) {
  101 + throw const MobileScannerException(
  102 + errorCode: MobileScannerErrorCode.controllerUninitialized,
  103 + errorDetails: MobileScannerErrorDetails(
  104 + message: 'The MobileScannerController has not been initialized.',
  105 + ),
  106 + );
  107 + }
  108 +
  109 + if (_isDisposed) {
  110 + throw const MobileScannerException(
  111 + errorCode: MobileScannerErrorCode.controllerDisposed,
  112 + errorDetails: MobileScannerErrorDetails(
  113 + message: 'The MobileScannerController was used after it has been disposed.',
  114 + ),
  115 + );
  116 + }
  117 + }
  118 +
97 /// Analyze an image file. 119 /// Analyze an image file.
98 /// 120 ///
99 /// The [path] points to a file on the device. 121 /// The [path] points to a file on the device.
@@ -166,9 +188,14 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { @@ -166,9 +188,14 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
166 188
167 @override 189 @override
168 Future<void> dispose() async { 190 Future<void> dispose() async {
  191 + if (_isDisposed) {
  192 + return;
  193 + }
  194 +
169 await MobileScannerPlatform.instance.dispose(); 195 await MobileScannerPlatform.instance.dispose();
170 unawaited(_barcodesController.close()); 196 unawaited(_barcodesController.close());
171 197
  198 + _isDisposed = true;
172 super.dispose(); 199 super.dispose();
173 } 200 }
174 } 201 }