After successfully a Barcode / Qrcode Scanner on Flutter VSCode, when I try to make apk file, I get an error

1.7k Views Asked by At

Code:

import 'package:flutter/material.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'dart:io';
import 'package:flutter/foundation.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);


  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  Barcode? result;
  QRViewController? controller;

  // In order to get hot reload to work we need to pause the camera if the platform
  // is android, or resume the camera if the platform is iOS.
  @override
  void reassemble() {
    super.reassemble();
    if (Platform.isAndroid) {
      controller!.pauseCamera();
    } else if (Platform.isIOS) {
      controller!.resumeCamera();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: <Widget>[
          Expanded(
            flex: 5,
            child: QRView(
              key: qrKey,
              onQRViewCreated: _onQRViewCreated,
            ),
          ),
          Expanded(
            flex: 1,
            child: Center(
              child: (result != null)
                  ? Text(
                  'Barcode Type: ${describeEnum(result!.format)}   Data: ${result!.code}')
                  : Text('Scan a code'),
            ),
          )
        ],
      ),
    );
  }

  void _onQRViewCreated(QRViewController controller) {
    this.controller = controller;
    controller.scannedDataStream.listen((scanData) {
      setState(() {
        result = scanData;
      });
    });
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }
}

Error:

flutter build apk --build-name=1.0.4 --build-number=4

 Building with sound null safety 

e: D:\flutter\.pub-cache\hosted\pub.dartlang.org\qr_code_scanner-0.7.0\android\src\main\kotlin\net\touchcapture\qr\flutterqr\QRView.kt: (23, 1): Class 'QRView' is not abstract and does not implement abstract member public abstract fun onRequestPermissionsResult(p0: Int, p1: Array<(out) String!>, p2: IntArray): Boolean defined in io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
e: D:\flutter\.pub-cache\hosted\pub.dartlang.org\qr_code_scanner-0.7.0\android\src\main\kotlin\net\touchcapture\qr\flutterqr\QRView.kt: (216, 26): Null can not be a value of a non-null type String
e: D:\flutter\.pub-cache\hosted\pub.dartlang.org\qr_code_scanner-0.7.0\android\src\main\kotlin\net\touchcapture\qr\flutterqr\QRView.kt: (247, 26): Null can not be a value of a non-null type String
e: D:\flutter\.pub-cache\hosted\pub.dartlang.org\qr_code_scanner-0.7.0\android\src\main\kotlin\net\touchcapture\qr\flutterqr\QRView.kt: (310, 5): 'onRequestPermissionsResult' overrides nothing
e: D:\flutter\.pub-cache\hosted\pub.dartlang.org\qr_code_scanner-0.7.0\android\src\main\kotlin\net\touchcapture\qr\flutterqr\QRViewFactory.kt: (10, 1): Class 'QRViewFactory' is not abstract and does not implement abstract base class member public abstract fun create(p0: Context?, p1: Int, p2: Any?): PlatformView defined in io.flutter.plugin.platform.PlatformViewFactory
e: D:\flutter\.pub-cache\hosted\pub.dartlang.org\qr_code_scanner-0.7.0\android\src\main\kotlin\net\touchcapture\qr\flutterqr\QRViewFactory.kt: (13, 5): 'create' overrides nothing

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':qr_code_scanner:compileReleaseKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3m 59s
Running Gradle task 'assembleRelease'...                          242.3s
Gradle task assembleRelease failed with exit code 1
2

There are 2 best solutions below

0
On

I built it on a first try. What can you try:

  • Set minSdkVersion to 20
  • run flutter clean
  • upgrade all dependencies
  • run flutter channel stable && flutter upgrade

My configuration:

Flutter 2.10.4 • channel stable • https://github.com/flutter/flutter.git

Framework • revision c860cba910 (8 days ago) • 2022-03-25 00:23:12 -0500

Engine • revision 57d3bac3dd

Tools • Dart 2.16.2 • DevTools 2.9.2

0
On

I had faced the same issue while attempting to launch the flutter app on an Android device. So I converted the previous version of qr_code_Scanner to the newer version as follows: qr_code_scanner:^0.6.1 to qr_code_scanner:^1.0.1 and it really works to me.