Navaron Bracke

lazily set up the barcode reader on web

@@ -25,7 +25,7 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -25,7 +25,7 @@ class MobileScannerWeb extends MobileScannerPlatform {
25 String? _alternateScriptUrl; 25 String? _alternateScriptUrl;
26 26
27 /// The internal barcode reader. 27 /// The internal barcode reader.
28 - final BarcodeReader _barcodeReader = ZXingBarcodeReader(); 28 + BarcodeReader? _barcodeReader;
29 29
30 /// The stream controller for the barcode stream. 30 /// The stream controller for the barcode stream.
31 final StreamController<BarcodeCapture> _barcodesController = 31 final StreamController<BarcodeCapture> _barcodesController =
@@ -221,11 +221,11 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -221,11 +221,11 @@ class MobileScannerWeb extends MobileScannerPlatform {
221 221
222 @override 222 @override
223 Widget buildCameraView() { 223 Widget buildCameraView() {
224 - if (!_barcodeReader.isScanning) {  
225 - return const SizedBox(); 224 + if (_barcodeReader?.isScanning ?? false) {
  225 + return HtmlElementView(viewType: _getViewType(_textureId));
226 } 226 }
227 227
228 - return HtmlElementView(viewType: _getViewType(_textureId)); 228 + return const SizedBox();
229 } 229 }
230 230
231 @override 231 @override
@@ -266,11 +266,13 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -266,11 +266,13 @@ class MobileScannerWeb extends MobileScannerPlatform {
266 throw PermissionRequestPendingException(); 266 throw PermissionRequestPendingException();
267 } 267 }
268 268
269 - await _barcodeReader.maybeLoadLibrary( 269 + _barcodeReader = ZXingBarcodeReader();
  270 +
  271 + await _barcodeReader?.maybeLoadLibrary(
270 alternateScriptUrl: _alternateScriptUrl, 272 alternateScriptUrl: _alternateScriptUrl,
271 ); 273 );
272 274
273 - if (_barcodeReader.isScanning) { 275 + if (_barcodeReader?.isScanning ?? false) {
274 throw const MobileScannerException( 276 throw const MobileScannerException(
275 errorCode: MobileScannerErrorCode.controllerAlreadyInitialized, 277 errorCode: MobileScannerErrorCode.controllerAlreadyInitialized,
276 errorDetails: MobileScannerErrorDetails( 278 errorDetails: MobileScannerErrorDetails(
@@ -292,7 +294,7 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -292,7 +294,7 @@ class MobileScannerWeb extends MobileScannerPlatform {
292 } 294 }
293 295
294 // Listen for changes to the media track settings. 296 // Listen for changes to the media track settings.
295 - _barcodeReader.setMediaTrackSettingsListener( 297 + _barcodeReader?.setMediaTrackSettingsListener(
296 _handleMediaTrackSettingsChange, 298 _handleMediaTrackSettingsChange,
297 ); 299 );
298 300
@@ -302,7 +304,7 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -302,7 +304,7 @@ class MobileScannerWeb extends MobileScannerPlatform {
302 304
303 _maybeFlipVideoPreview(_videoElement, videoStream); 305 _maybeFlipVideoPreview(_videoElement, videoStream);
304 306
305 - await _barcodeReader.start( 307 + await _barcodeReader?.start(
306 startOptions, 308 startOptions,
307 videoElement: _videoElement, 309 videoElement: _videoElement,
308 videoStream: videoStream, 310 videoStream: videoStream,
@@ -318,7 +320,7 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -318,7 +320,7 @@ class MobileScannerWeb extends MobileScannerPlatform {
318 } 320 }
319 321
320 try { 322 try {
321 - _barcodesSubscription = _barcodeReader.detectBarcodes().listen( 323 + _barcodesSubscription = _barcodeReader?.detectBarcodes().listen(
322 (BarcodeCapture barcode) { 324 (BarcodeCapture barcode) {
323 if (_barcodesController.isClosed) { 325 if (_barcodesController.isClosed) {
324 return; 326 return;
@@ -328,15 +330,15 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -328,15 +330,15 @@ class MobileScannerWeb extends MobileScannerPlatform {
328 }, 330 },
329 ); 331 );
330 332
331 - final bool hasTorch = await _barcodeReader.hasTorch(); 333 + final bool hasTorch = await _barcodeReader?.hasTorch() ?? false;
332 334
333 if (hasTorch && startOptions.torchEnabled) { 335 if (hasTorch && startOptions.torchEnabled) {
334 - await _barcodeReader.setTorchState(TorchState.on); 336 + await _barcodeReader?.setTorchState(TorchState.on);
335 } 337 }
336 338
337 return MobileScannerViewAttributes( 339 return MobileScannerViewAttributes(
338 hasTorch: hasTorch, 340 hasTorch: hasTorch,
339 - size: _barcodeReader.videoSize, 341 + size: _barcodeReader?.videoSize ?? Size.zero,
340 ); 342 );
341 } catch (error, stackTrace) { 343 } catch (error, stackTrace) {
342 throw MobileScannerException( 344 throw MobileScannerException(
@@ -359,7 +361,8 @@ class MobileScannerWeb extends MobileScannerPlatform { @@ -359,7 +361,8 @@ class MobileScannerWeb extends MobileScannerPlatform {
359 await _barcodesSubscription?.cancel(); 361 await _barcodesSubscription?.cancel();
360 _barcodesSubscription = null; 362 _barcodesSubscription = null;
361 363
362 - await _barcodeReader.stop(); 364 + await _barcodeReader?.stop();
  365 + _barcodeReader = null;
363 } 366 }
364 367
365 @override 368 @override