Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jaca420 opened this issue Nov 20, 2022 · 1 comment

Comments

@jaca420
Copy link

jaca420 commented Nov 20, 2022

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 jaca420 closed this as completed Nov 20, 2022
@jaca420
Copy link
Author

jaca420 commented Nov 20, 2022

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.

@jaca420 jaca420 reopened this Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant