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
744 stars 444 forks source link

[Quite Detailed] No Input box ,no log nor any error #1035

Open airai-ad-117 opened 3 weeks ago

airai-ad-117 commented 3 weeks ago

Hello ,

Note :I have used the package ,once before too ,for a freelance app ,when the MobileScanner has onDectect parameter ,most probably v4 ,so i know ,what should have been the correct result.

Code :

class _scan__page extends StatefulWidget {
  const _scan__page({
    required this.barcode__handle,
  });

  final void Function(string) barcode__handle /*
called only once ,for the first `scan:result` */
      ;

  @override
  State<_scan__page> createState() => __scan__pageState();
}

class __scan__pageState extends State<_scan__page> {
  final controller = MobileScannerController(
    detectionSpeed: DetectionSpeed.noDuplicates,
    facing: CameraFacing.back,
  );

  string? scan__result;

  @override
  void initState() {
    super.initState();

    /*Timer(
        Duration(
          milliseconds: 100,
        ), () {
      if (context.valid__ok()) {
        controller.setZoomScale(.5);
      }
    });*/

    controller.barcodes.listen(
      barcode__handle,
    );
  }

  @override
  void dispose() {
    controller.dispose();
    scan__result = null;

    super.dispose();
  }

  void barcode__handle(
    final BarcodeCapture barcodes,
  ) {
    print('barcodes.raw: ${barcodes.raw}');
    print('barcodes.size: ${barcodes.size}');
    print('barcodes_.length: ${barcodes.barcodes.length}');

    for (final barcode in barcodes.barcodes) {
      print('  barcode.rawValue: ${barcode.rawValue}');
      print('  barcode.rawBytes: ${barcode.rawBytes}');
      print('  barcode.type: ${barcode.type}');
      print('  barcode.format: ${barcode.format}');
      print('  barcode.url: ${barcode.url}');

      final rawValue = barcode.rawValue;
      print('  barcode.rawValue: $rawValue');

      if ((scan__result == null) && //
          (rawValue != null)) {
        scan__result = rawValue;

        context.navigate__backward();
        widget.barcode__handle(rawValue);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Scan"),
      ),
      body: Center(
        child: Padding(
          padding: EdgeInsets.all(12.px),
          child: ClipRRect(
            borderRadius: BorderRadius.all(
              Radius.circular(12.px),
            ),
            child: MobileScanner(
              controller: controller,
            ),
          ),
        ),
      ),
    );
  }
}

If i un-comment the initState's Timer call ,the error/throw(on terminal) is :

I/flutter (26304):                                          <MobileScannerException>MobileScannerException: code controllerUninitialized, message: The MobileScannerController has not been initialized.: <_StackTrace>#0      MobileScannerController._throwIfNotInitialized (package:mobile_scanner/src/mobile_scanner_controller.dart:150:7)
I/flutter (26304): #1      MobileScannerController.setZoomScale (package:mobile_scanner/src/mobile_scanner_controller.dart:211:5)
I/flutter (26304): #2      __scan__pageState.initState.<anonymous closure> (package:qr_app/scan.dart:258:20)
I/flutter (26304): #3      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
I/flutter (26304): #4      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
I/flutter (26304): #5      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
I/flutter (26304): #6      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

I also tried running in release mode ,but no change.

When navigating to the scan page ,the app neither ask for any permission ,in v4 it did. Although ,the app's info page in system-settings of android-phone ,have camera permission ,and even after [manually(by going to settings app ,then app info ,then permissions ,then grant)] granting the permission ,no change.

I only tried on android-phone ,not apple-phone

The app's pubspec :


environment:
  sdk: '>=3.3.0 <4.0.0'

dependencies:
  flutter:
    sdk: flutter
  base:
    path: ../base
  flutter_base:
    path: ../base__flutter
  cupertino_icons: ^1.0.6
  path_provider: ^2.1.2
  url_launcher: ^6.2.5
  share_plus: ^8.0.3
  account_picker: ^2.1.0
  android_intent_plus: ^5.0.2
  flutter_native_splash: ^2.4.0
  image_picker_android: ^0.8.10
  image_picker: ^1.1.0
  mobile_scanner: ^5.0.1
  barcode_widget: ^2.0.4

thanks

navaronbracke commented 3 weeks ago

As stated in the documentation, starting from version 5.0.0, you need to explicitly manage the lifecycle of the MobileScannerController. You are missing a call to MobileScannerController.start(). See our examples for some runnable code samples.

For example: https://github.com/juliansteenbakker/mobile_scanner/blob/master/example/lib/barcode_scanner_controller.dart