Flutter Barcode Plugin for Linux: from Dart to C++

For the past several weeks, the Flutter plugin of Dynamsoft Barcode Reader has covered Windows, Android and Web. This week, we go through the plugin implementation for Linux. Similar to Windows, we combine Dart and C++ code for Linux. The unexpected thing is the C++ data types of Flutter defined for Linux change a lot. So it still takes a little bit of time to make the plugin work.

What You Should Know About Dynamsoft Barcode Reader

Learning Resources for Flutter Desktop Development

Step-by-step Flutter Linux Plugin Development

To initialize the plugin template for Linux, we need to switch the coding environment to Linux firstly, and then run the following command to add the template to the existing plugin project:

Here is the Linux plugin folder generated by the above command:

It is a CMake project as well. We need Dynamsoft Barcode Reader libraries for linking. Therefore, the next step is to create a lib folder and copy DynamsoftBarcodeReader/Lib/Linux/*.so files to the folder. Afterwards, we configure link_directories, target_link_libraries and flutter_barcode_sdk_bundled_libraries in CMakeLists.txt:

So far, the build configuration is settled. Let’s get started to code.

We open flutter_barcode_sdk_plugin.cc and find the flutter_barcode_sdk_plugin_handle_method_call() function.

The method name should be familiar to you if you have built plugins for other platforms.

Referring to the methods implemented for Windows, we can construct the skeleton:

Since the code logic is almost the same to Windows, as long as we know the data types and corresponding calling methods, it will be easy to implement the plugin for Linux. The relevant header file is located at example/linux/flutter/ephemeral/flutter_linux/fl_value.h.

According to the comments of the header file, we can get the ways of converting Dart arguments to C++ types for Linux:

  • String
  • Integer
  • Bytes

In the meantime, the function return type and data wrapping method can be changed in barcode_manager.h as follows:

Now, the Flutter barcode plugin for Linux has been finished. Since the Dart code for Windows also works for Linux, there is no extra code needed for the example project. We just need to run the example directly by setting Linux as the device:

The tutorial of how to build Flutter barcode plugin for iOS and macOS is coming next week.

Flutter Barcode SDK Download


Source Code


Originally published at https://www.dynamsoft.com on May 19, 2021.

Manager of Dynamsoft Open Source Projects | Tech Lover