pdliuw / ai_barcode

Barcode generation,Barcode scanning,qrcode,qrcode generation,qrcode creator,flutter barcode,flutter qrcode,support android iOS web platform
BSD 3-Clause "New" or "Revised" License
123 stars 81 forks source link

LateInitializationError: Field '_instance' has not been initialized on Flutter web app #77

Open jaca420 opened 1 year ago

jaca420 commented 1 year ago

Hello,

I tried adding the ai_barcode plugin to an existing Flutter web application. I need to scan barcodes, NOT qr codes.

After building and deploying I get some errors.

I'm using ai_barcode: ^3.2.4


import 'package:flutter/material.dart';
import 'package:nftgame/Screens/app_barcode_scanner_widget.dart';

class CustomSizeScannerPage extends StatefulWidget {
  const CustomSizeScannerPage({super.key});

  @override
  CustomSizeScannerPageState createState() => CustomSizeScannerPageState();
}

class CustomSizeScannerPageState extends State<CustomSizeScannerPage> {
  String _code = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_code),
      ),
      body: Column(
        children: [
          Expanded(
            flex: 4,
            child: AppBarcodeScannerWidget.defaultStyle(
              resultCallback: (String code) {
                setState(() {
                  _code = code;
                });
              },
            ),
          ),
          Expanded(
            flex: 1,
            child: Container(),
          ),
        ],
      ),
    );
  }
}
import 'package:ai_barcode/ai_barcode.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

late String label;
late Function(String result) _resultCallback;

class AppBarcodeScannerWidget extends StatefulWidget {
  AppBarcodeScannerWidget.defaultStyle({
    super.key,
    Function(String result)? resultCallback,
    String label = '',
  }) {
    _resultCallback = resultCallback ?? (String result) {};
    label = label;
  }

  @override
  AppBarcodeState createState() => AppBarcodeState();
}

class AppBarcodeState extends State<AppBarcodeScannerWidget> {
  bool isGranted = false;

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

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      TargetPlatform platform = Theme.of(context).platform;
      if (!kIsWeb) {
        if (platform == TargetPlatform.android ||
            platform == TargetPlatform.iOS) {
          _requestMobilePermission();
        } else {
          setState(() {
            isGranted = true;
          });
        }
      } else {
        setState(() {
          isGranted = true;
        });
      }
    });
  }

  void _requestMobilePermission() async {
    bool isGrated = true;
    if (await Permission.camera.status.isGranted) {
      isGrated = true;
    } else {
      if (await Permission.camera.request().isGranted) {
        isGrated = true;
      }
    }
    setState(() {
      isGranted = isGrated;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(child: _BarcodeScannerWidget()),
      ],
    );
  }
}

class _BarcodeScannerWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _AppBarcodeScannerWidgetState();
  }
}

class _AppBarcodeScannerWidgetState extends State<_BarcodeScannerWidget> {
  late ScannerController _scannerController;

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

    _scannerController = ScannerController(scannerResult: (result) {
      _resultCallback(result);
    }, scannerViewCreated: () {
      TargetPlatform platform = Theme.of(context).platform;
      if (TargetPlatform.iOS == platform) {
        Future.delayed(const Duration(seconds: 2), () {
          _scannerController.startCamera();
          _scannerController.startCameraPreview();
        });
      } else {
        _scannerController.startCamera();
        _scannerController.startCameraPreview();
      }
    });
  }

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

    _scannerController.stopCameraPreview();
    _scannerController.stopCamera();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: _getScanWidgetByPlatform(),
        ),
      ],
    );
  }

  Widget _getScanWidgetByPlatform() {
    return PlatformAiBarcodeScannerWidget(
      platformScannerController: _scannerController,
    );
  }
}
LateInitializationError: Field '_instance' has not been initialized.
Another exception was thrown: Instance of 'minified:jn<void>'
main.dart.js:28412 Another exception was thrown: Instance of 'minified:jn<void>'
main.dart.js:28412 Another exception was thrown: Instance of 'minified:jn<void>'
main.dart.js:28412 
jaca420 commented 1 year ago

Sorry ... had another barcode scanner. After removing it the plugin is working. But it's not doing anything. The title of the screen title: Text(_code), doesn't change. QrCode or BarCode are not scanned.