juliansteenbakker / mobile_scanner

A universal scanner for Flutter based on MLKit. Uses CameraX on Android and AVFoundation on iOS.
BSD 3-Clause "New" or "Revised" License
756 stars 446 forks source link

Bloc Provider triggered at the second detection #982

Closed carlodenardin closed 2 months ago

carlodenardin commented 2 months ago

I'm using mobile_scanner In my app and I use Bloc to manage the states. I do not know why the Event is not triggered at the first scan, the scanner works and I can also print the rawValue of the barcode. The problem is that the ScannerCheckQRCode is not triggered but if I scan the qrcode a second time it is triggered but I get this error: [VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: on<ScannerCheckQRCode> was called multiple times. There should only be a single event handler per event type.

MobileScanner(
  controller: mobileScannerController,
  onDetect: (capture) {
    context.read<ScannerBloc>().add(
          ScannerCheckQRCode(clientId: 'user_id'),
        );
  },
),

I tried to use the controller with the noDuplicates but It do not trigger the event the first time so after that the qrcode is not detected anymore.

navaronbracke commented 2 months ago

What version of the plugin do you use?

carlodenardin commented 2 months ago

@navaronbracke 4.0.1, The second time in which I scan the QRCode with DetectionSpeed.normal the error appears and the bloc event is triggered.

navaronbracke commented 2 months ago

The error

Bad state: on<ScannerCheckQRCode> was called multiple times. There should only be a single event handler per event type

indicates an error with your BloC implementation?

For example, the code sample on https://pub.dev/packages/bloc only sets an on<T> event handler in the constructor of the CounterBloc, which is only created once. The documentation specifies this as well:

Register event handler for an event of type E. There should only ever be one event handler per event type E.