I tried to scan a QR code with barcode_scan
using Flutter with the code below, but I am getting errors all the time: unknown error type 'ScanResult' is not a subtype of type 'String' in typecast Scan
.
Could someone give me an explanation or could someone has an idea about it?
import 'package:barcode_scan/gen/protos/protos.pbserver.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/services.dart';
import 'package:qr_flutter/qr_flutter.dart';
class Scanner extends StatefulWidget {
@override
_ScannerState createState() => _ScannerState();
}
class _ScannerState extends State<Scanner> {
String result = "press the camera to start the scan !";
Future _scanQR() async {
try {
String qrResult = await BarcodeScanner.scan() as String;
setState(() {
result = qrResult;
});
} on PlatformException catch (ex) {
if (ex.code == BarcodeScanner.cameraAccessDenied) {
setState(() {
result = "Camera was denied";
});
} else {
setState(() {
result = "Unknown Error $ex";
});
}
} on FormatException {
setState(() {
result = "You pressed the back button before scanning anything";
});
} catch (ex) {
setState(() {
result = "Unknown Error $ex";
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("QR Scanner"),
),
body: Center(
child: Text(
result,
style: new TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
),
),
floatingActionButton: FloatingActionButton.extended(
icon: Icon(Icons.camera_alt),
label: Text("Scan"),
onPressed: _scanQR,
),
);
}
}
You can use the rawContent
property to access the data as a String
:
ScanResult qrScanResult = await BarcodeScanner.scan();
String qrResult = qrScanResult.rawContent;
Follows the full example:
import 'package:barcode_scan/gen/protos/protos.pbserver.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/services.dart';
import 'package:qr_flutter/qr_flutter.dart';
class Scanner extends StatefulWidget {
@override
_ScannerState createState() => _ScannerState();
}
class _ScannerState extends State<Scanner> {
String result = "press the camera to start the scan !";
Future _scanQR() async {
try {
ScanResult qrScanResult = await BarcodeScanner.scan();
String qrResult = qrScanResult.rawContent;
setState(() {
result = qrResult;
});
} on PlatformException catch (ex) {
if (ex.code == BarcodeScanner.cameraAccessDenied) {
setState(() {
result = "Camera was denied";
});
} else {
setState(() {
result = "Unknown Error $ex";
});
}
} on FormatException {
setState(() {
result = "You pressed the back button before scanning anything";
});
} catch (ex) {
setState(() {
result = "Unknown Error $ex";
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("QR Scanner"),
),
body: Center(
child: Text(
result,
style: new TextStyle(fontSize: 30.0, fontWeight: FontWeight.bold),
),
),
floatingActionButton: FloatingActionButton.extended(
icon: Icon(Icons.camera_alt),
label: Text("Scan"),
onPressed: _scanQR,
),
);
}
}