Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter qr_code_scanner on successfully scanned data update navigation?

Tags:

flutter

dart

This is a widget, that have function that triggers each time we scan qr code.

  import 'package:qr_code_scanner/qr_code_scanner.dart';

 @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: <Widget>[
              Expanded(
                flex: 4,
                child: QRView(
                  key: qrKey,
                  onQRViewCreated: _onQRViewCreated,
                  overlay: QrScannerOverlayShape(
                    borderColor: Colors.red,
                    borderRadius: 10,
                    borderLength: 30,
                    borderWidth: 10,
                    cutOutSize: 300,
                  ),
                ),
              ),

In function I want to navigate to next screen.

  void _onQRViewCreated(QRViewController controller) {
    this.controller = controller;
    controller.scannedDataStream.listen((scanData) {
           Navigator.push(
           context,
          MaterialPageRoute(builder: (context) => SecondRoute()),
         );
    });
  }

Problem here is that that listen event trigger many times, is it possible to stop this function after first successfully scan data? I try with

controller.scannedDataStream.first;

But that return empty string and not triggers when real data are scanned.

I need to click 40 times to go back from Second Route to return to QR scanner widget. Thanks!

like image 817
user13034054 Avatar asked Dec 03 '22 17:12

user13034054


1 Answers

for future reference i found pausing the camera works better :)

void _onQRViewCreated(QRViewController controller) {
this.controller = controller;
controller.scannedDataStream.listen((scanData) {
    qrText = scanData;
    SecondPageRoute();
   });
 }


SecondPageRoute() async {
       controller?.pauseCamera();
       var value = await Navigator.push(context,
           MaterialPageRoute(builder: (context) {
             return SecondPage(qrText);
           })).then((value) => controller.resumeCamera());
}
like image 169
Kurtus Avatar answered Feb 02 '23 00:02

Kurtus