How to get barcode from PDA scanner device in flutter

0 votes

My app installs on types of PDA devices (like datalogic, Honeywell, zkc, etc.) I want to get the barcode scanned from these devices. So about this issue, I use RawKeyboardListener like the following code:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(const MyApp());

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

  static const String _title = 'RawKeyboardListener';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyWidget(),
      ),
    );
  }
}

class MyWidget extends StatefulWidget {
  const MyWidget({Key? key}) : super(key: key);

  @override
  State<MyWidget> createState() => MyWidgetState();
}

class MyWidgetState extends State<MyWidget> {
  final FocusNode _focusNode = FocusNode();

  String _chars = '';

  String? barcode;

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    FocusScope.of(context).requestFocus(_focusNode);
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      child: RawKeyboardListener(
        focusNode: _focusNode,
        onKey: (RawKeyEvent event) {
          if (event is RawKeyDownEvent || event is RawKeyUpEvent) {
            if (event.physicalKey == PhysicalKeyboardKey.enter) {
              setState(() {
                barcode = _chars;
                _chars = '';
              });
            } else {
              _chars += event.data.keyLabel;
            }
          }
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Barcode: $barcode',
            ),
          ],
        ),
      ),
    );
  }
}

This code invokes an onKey callback when scanning the barcode via the PDA scanner, But the barcode (event.data.keyLabel) is empty on datalogic PDA scanner device.

This code works with a handheld scanner as true. I saw this question but it doesn't work for me.

How do I get barcodes from all types of PDA scanners?

Apr 3, 2023 in Flutter by sarit
• 1,830 points
1,942 views

1 answer to this question.

0 votes

It's possible that the data sent by the PDA scanner is not being recognized as key events by the RawKeyboardListener widget. In this case, you may need to use a specialized package for barcode scanning in Flutter.

One popular package for barcode scanning is the flutter_barcode_scanner package. Here's how you can use it in your app:

  • Add the flutter_barcode_scanner package to your pubspec.yaml file:
dependencies:
  flutter_barcode_scanner: ^2.0.0
  • Import the package in your Dart code:
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
  • Use the FlutterBarcodeScanner.scanBarcode method to scan the barcode:
String barcode = await FlutterBarcodeScanner.scanBarcode(
    '#ff6666', 'Cancel', true, ScanMode.BARCODE);

This method shows a fullscreen scanner that captures the barcode and returns the scanned value as a string. You can then use this value in your app as needed.

Note that this package supports a wide range of barcode formats and can be used with various types of PDA devices.

I hope this helps!

To know more, join our Flutter Certification today.

answered Apr 3, 2023 by seena

Related Questions In Flutter

0 votes
1 answer

How to Deserialize a list of objects from json in flutter Ask Question?

To deserialize a list of objects from ...READ MORE

answered Mar 28, 2023 in Flutter by vishalini
2,281 views
0 votes
1 answer

How to change Android minSdkVersion in Flutter Project?

Yes, you can change the minSdkVersion in ...READ MORE

answered Mar 21, 2023 in Flutter by Tej
11,084 views
0 votes
1 answer

How to implement drop down list in flutter?

You're close! The error you're getting is ...READ MORE

answered Mar 26, 2023 in Flutter by vishalini
4,085 views
0 votes
2 answers

How to create a circle icon button in Flutter?

To create something similar to a FloatingActionButton, ...READ MORE

answered Aug 23, 2023 in Flutter by anonymous
• 140 points
14,351 views
0 votes
1 answer

Dart_LoadScriptFromKernel: The binary program does not contain 'main'.

Hi@akhtar, You need to add the main() function ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
3,553 views
0 votes
1 answer

How to install Dart in Windows system?

Hi@akhtar, Dart is the programming language used to code Flutter apps. ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
1,109 views
0 votes
1 answer

flutter run: No connected devices

Hi@akhtar, To prepare to run and test your ...READ MORE

answered Jul 21, 2020 in Others by MD
• 95,460 points
4,116 views
0 votes
1 answer

How to create a function in Dart language?

Hi@akhtar, There are many function types available in ...READ MORE

answered Jul 22, 2020 in Others by MD
• 95,460 points
748 views
0 votes
1 answer

How to prevent deprecated flutter widgets suggestions in vs code?

To prevent deprecated Flutter widgets suggestions in ...READ MORE

answered Apr 6, 2023 in Flutter by vinayak
1,057 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP