How to Use Dart with Barcode RESTful Web Service

A few months ago, Google announced a new operating system named Fuchsia. It was my first time to hear about Dart, which is the primary programming language for Fuchsia. Dart is a programming language designed by Google.  It is used to build the web (Dart webdev), mobile (Flutter) and server (Dart VM) applications. In this article, I will share how to write my first Dart console application with Dynamsoft barcode RESTful web service.

Setting Up the Dart Development Environment

Install Dart SDK, which includes Dart VM, libraries, package manager and more.

Install IDE IntelliJ IDEA. The community edition is free. When launching the IDE first time, click menu File > Settings > Plugins to install Dart plugin.

IntelliJ Dart Plugin

Dart Console Application with Barcode RESTful Web Service

Create a new console application project:

Dart Console Application Project

Using libraries

Here are the required libraries:

import 'dart:io';
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart' as http;

The dart: scheme is for built-in libraries, and the package: scheme is for external libraries provided by a package manager such as the pub tool.

You may see a prompt message Target of URI doesn’t exist. To use the external libraries, open pubspec.yaml and add dependencies:

name: barcode
version: 0.0.1
description: A simple console application.

Then click Get Dependencies to download the libraries.

Read a file and convert bytes to base64

Reading file as bytes is pretty easy:

var bytes = await new File(filename).readAsBytes();

If you have read the Dart tutorial, you may have noticed the code for base64 conversion:

import 'package:crypto/crypto.dart';
String base64 = CryptoUtils.bytesToBase64(bytes);

However, after getting the latest crypto package, I got the error Undefined name CryptoUtils.

I searched Dart APIs for “base64”, and found the following substitute:

import 'dart:convert';
String base64 = BASE64.encode(bytes);

Send JSON to RESTful web service

Construct the JSON data:

String jsonData = '{"image":"$base64","barcodeFormat":234882047,"maxNumPerPage":1}';

The value of barcode format represents all barcode types including Code 39, Code 93, Code 128, Codabar, Interleaved 2 of 5, EAN-8, EAN-13, UPC-A, UPC-E, Industrial 2 of 5, QR code, Datamatrix, and PDF417. I got this value from Dynamsoft barcode web service sample.

Send HTTP request and wait for the responded result:

var response = await, headers: {'Content-Type': 'application/json'}, body: jsonData);
print("Response status: ${response.statusCode}");
if (response.statusCode != 200) {
  print("Server Error !!!");
print("Response body: ${response.body}");

Map result = JSON.decode(response.body);
print("Barcode result: " + result['barcodes'][0]['displayValue']);


Run the Dart console application with the test barcode image:

Dart barcode RESTful web service

Source Code