0

I have a Flutter project. In that project I have a barcode scanner. When I scan a barcode, I return a result of numbers. In the same project, I have a local JSON file. In the local JSON file I have a field called 'BARCODE' and it contains some product's barcode number. I want to compare result and 'BARCODE' and if there is a match I want to bring the record from my JSON file. Any idea how to do that?

Here is my code:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;
import 'dart:convert';
import 'package:flutter/services.dart';

// ignore: camel_case_types
class barcodeReader extends StatefulWidget {
  _barcodeReaderState createState() => _barcodeReaderState();
}

// ignore: camel_case_types
class _barcodeReaderState extends State<barcodeReader> {
  String _title = 'Barcode Scanner';
  String _result = 'Click Scan button for start to scan';
  String _scanButtonLabel = 'Scan';
  String _scanResult = '(Empty result)';
  String _errorMessage = 'Unknown error';
  List data;
  Future<String> resultBarcode;
  Future<String> resultIlac;

  Future _scanCode() async {
    try {
      _scanResult =
          await FlutterBarcodeScanner.scanBarcode("#000000", "Cancel", true);

      setState(() {
        _result = _scanResult;
      });
    } catch (e) {
      _errorMessage = '$_errorMessage $e';

      setState(() {
        _result = _errorMessage;
      });
    }
  }

  Future<String> loadJsonData(int index, snapshot) async {
    var jsonText = await rootBundle.loadString('assets/csvjson_2son.json');
    var mydata = JsonDecoder().convert(snapshot.data.toString());
    setState(() => data = json.decode(jsonText));
    mydata[index]['BARKOD'] = resultBarcode;
    mydata[index]['ILAC ADI'] = resultIlac;
    if (resultBarcode == _result) {
      return resultIlac;
    }
  }

  @override
  void initState() {
    super.initState();
    this.loadJsonData(0, data);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 90,
        backgroundColor: Color(0xFF3EB16F),
        title: Text("Barcode Scanner"),
        centerTitle: true,
        textTheme: TextTheme(
          headline6: TextStyle(fontSize: 44, fontFamily: "Angel"),
        ),
      ),
      body: Center(
        child: Text('$_result'),
      ),
      floatingActionButton: FloatingActionButton.extended(
        icon: Icon(Icons.scanner),
        label: Text('$_scanButtonLabel'),
        onPressed: _scanCode,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
}


sinergy
  • 39
  • 6

1 Answers1

0

For a clear solution you need to share part of json file. Anyway in a map you can check if a key exist or not with;(if exists it returns bool value true)

body.containsKey("somebarcodenumber");// push your key

Check if the map contains a value ; (if exists it returns bool value true)

bool exists ;
exists = body.containsValue("someilacname");

Note : Key is the field name of map and value represents the value of a field.

   Map body = {"locale":"tr","conversationId":"123456ayx5","binNumber":"$binNumber"} ;

"locale" is a key , "tr" is its value.

update: you are mistaking when try to get the value to field. You attempt to update json file value.

mydata[index]['BARKOD'] = resultBarcode;
mydata[index]['ILAC ADI'] = resultIlac;

use it;

     mydata.forEach((element) { 
        if(element['BARKOD']==_result)
{
return element['ILAC ADI'];
}
      });
Muhtar
  • 1,506
  • 1
  • 8
  • 33
  • Part of my json is that. But I want to bring ILAC ADI field to the screen if they are equal `{ "BARKOD": 8699565643597, "ATC KODU": "A01AD02", "ATC ADI": "benzydamine", "REFERANS \nESDEGER": "REFERANS", "ESDEGERI": 1, "ILAC ADI": "TANTUM VERDE % 0,15 120 ML GARGARA", "ETKIN MADDE": "BENZIDAMIN HCL", "FIRMA ADI": "CINAY KIMYA", "BIRIM MIKTAR": "0,15%", "BIRIM CINSI": "G", "AMBALAJ MIKTARI": 120, "RECETE": "NORMAL RECETE", "KDV DAHIL PERAKENDE SATIS TL FIYATI": "3,67" },` – sinergy May 24 '21 at 20:05
  • Updated the comment – Muhtar May 24 '21 at 20:59