I am updating an old project. This qr code scanner is giving an error.The project had previously run on flutter 2.10. There are errors in Flutter 3.0.
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
Warning: unexpected element (uri:"", local:"base-extension"). Expected elements are <{}codename>,<{}layoutlib>,<{}api-level>
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/qr_code_scanner-0.7.0/lib/src/qr_code_scanner.dart:68:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/binding.dart').
WidgetsBinding.instance!.addObserver(_observer);
^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/qr_code_scanner-0.7.0/lib/src/qr_code_scanner.dart:86:20: Warning: Operand of null-aware operation '!' has type 'WidgetsBinding' which excludes null.
- 'WidgetsBinding' is from 'package:flutter/src/widgets/binding.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/binding.dart').
WidgetsBinding.instance!.removeObserver(_observer);
^
e: C:\src\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: C:\src\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: C:\src\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: C:\src\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: C:\src\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: C:\src\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:compileDebugKotlin'.
> 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 21s
Running Gradle task 'assembleDebug'... 22.5s
Exception: Gradle task assembleDebug failed with exit code 1
flutter doctor -v
[√] Flutter (Channel stable, 3.0.0, on Microsoft Windows [Version 10.0.19043.1645], locale en-US)
• Flutter version 3.0.0 at C:\src\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision ee4e09cce0 (2 days ago), 2022-05-09 16:45:18 -0700
• Engine revision d1b9a6938a
• Dart version 2.17.0
• DevTools version 2.12.2
Checking Android licenses is taking an unexpectedly long time...[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
• Android SDK at C:\Users\User\AppData\Local\Android\sdk
• Platform android-32, build-tools 32.1.0-rc1
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 11.0.11 9-b60-7590822)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Professional 2022 17.1.3)
• Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Professional
• Visual Studio Professional 2022 version 17.1.32328.378
• Windows 10 SDK version 10.0.19041.0
[√] Android Studio (version 2021.1)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.11 9-b60-7590822)
[√] VS Code (version 1.67.1)
• VS Code at C:\Users\User\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.40.0
[√] Connected device (5 available)
• SM T500 (mobile) • R9AR702P83X • android-arm64 • Android 11 (API 30)
• sdk gphone x86 (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19043.1645]
• Chrome (web) • chrome • web-javascript • Google Chrome 101.0.4951.54
• Edge (web) • edge • web-javascript • Microsoft Edge 101.0.1210.39
[√] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
What should I do to make it work? Before everything was fine, I updated my project to the latest version of flutter and now I get this error.
CodePudding user response:
Downgrade the flutter to the previous version "flutter downgrade" until an upgrade to flutter 3.0.0 provided by owners of all packages you are used in your project .
CodePudding user response:
You can directly import the library in this way:
qr_code_scanner:
git:
url: https://github.com/xeinebiu/qr_code_scanner.git
ref: fix_break_changes_platform