Copyright notice: This article is the blogger’s original article, shall not be reproduced without the permission of the blogger. Juejin. Cn/post / 684490…

Reprint please indicate the source: juejin.cn/post/684490… This post is from AWeiLoveAndroid’s blog


This article was first published on the public account Flutter. More information about Flutter is welcome.


Dart2.6, the latest version of Dart, has been released. What’s new this time? Let’s take a look.

Dart.dev /platforms Dart provides a rich compiler for different system platforms: “Flutter” for Android and iOS, “Flutter Web” for Web development, “Flutter” for desktop (this feature is experimental), use AngularDart angulardart.dev/ for Web development, etc., All without the Dart compiler.

The Dart team recently released the latest version of the Dart feature update, the biggest highlight is: “Dart 2 Native.” It is an extension of an existing compiler that compilers Dart programs into self-contained executables containing pre-compiled (AOT) machine code. With Dart 2 Native, you can use Dart to create tools from the command line on macOS, Windows, or Linux. An illustration of this feature is shown below:


Dart Native and Dart 2 Native compilers

Dart has supported AOT (precompiled) compilation to Native machine code for many years, so Dart Native is a fairly mature technology. However, Dart has only released this feature on iOS and Android mobile devices with Flutter in the past.

Now with dart2native, native build support has been extended to support traditional desktop operating systems running macOS, Windows and Linux. Because executables created using Dart 2 Native are standalone, they can run on computers that do not have the Dart SDK installed. And because they are compiled using Dart’s AOT compiler, the executable starts running in milliseconds. As with other Dart compilers and runtimes, the same set of core libraries can be used in Dart when compiling into native code.

If you have used dart2aot before, then starting with Dart2.6, you will use dart2native, which is a superset of dart2aot and is more complete than dart2aot.


Dart2.6 download and install

Dart 2.6 is available on dart’s website at dart. Dev /get-dart

Dart.dev /tools/ SDK /a… In Stable Channel, select your operating system (Windows, Linux, Mac) and download the corresponding files according to the architecture of your computer. For example, X64 is 64-bit and IA32 is 32-bit.

Decompress the file, select an English installation path (not the Flutter installation path, otherwise there will be errors), add the installation path to the environment variable “path”, reboot your computer, and use the Dart command line.

Note: The Dart SDK installation is inconsistent with the Dart SDK built into Flutter and cannot be shared. You need to set separate installation folders for storage.

Run dart –version to view the DART version. Run where dart to view the DART installation path. As shown below:


3. Compile the command line application using Dart2native

Dart2native can be used to build and deploy command-line programs. Libraries such as dart: IO (basic I/O), Package: HTTP (network operations), Package: ARgs (parameter parsing) are typically used. Here is a simple example of dart2Native compiling the “Hello, world” application into an executable:

Dart source code:

The main () {print('Hello Dart'); }Copy the code

Compile hello.dart into the hello executable:

dart2native hello.dart -o hello
Copy the code

The compilation process is as follows:


Dart 2 Native’s enhancement and compatibility with DART: FFI

Native applications typically require access to native API functionality from the operating system. These system apis typically exist in native C-based libraries, and Dart supports interactivity with these libraries through Dart: FFI, a new Dart and C interaction feature introduced in Dart 2.5 preview.

Dart2.6 made significant changes to many of the DART: FFI apis, making our API easy to use, providing more type safety, and providing easy access to memory.

The Dart 2 Native compiler is compatible with DART: FFI, so you can create and compile native DART applications that use it.

A member of the Dart team recently started using Dart: FFI to create a dart_console library (pub.dev/packages/da…) for console application development. , it has functions such as getting window dimensions, reading and setting cursor positions, managing colors, reading keys, and controlling sequences. Dart capabilities: FFI makes DART a very powerful console application language. Dart_console is the Dart version implementation of the Kilo text editor. Kilogithub.com/antirez/kil… The library is written in C and contains about 1000 lines of code.


Write a code editor of 7M with less than 500 lines of code

Using the Dart core library, Dart: FFI, and dart_Console libraries, we can create very interesting console applications. The Dart_Console library includes a complete Kilo demo, which is a console text editor written with about 500 lines of Dart code.

Using the new dart2native compiler, we could easily package it and end up with a 7MB self-contained code editor. Here is a demo of a compiled editor, then using the compiled editor to edit your own source code to fix errors:


6. Dart2native server build performance is greatly improved

For a serverless back end, fast service startup is critical. Dart based services traditionally run with our JIT (just-in-time) compiler, but JIT based execution has a high latency at startup because it needs to be compiled and warmed up before it can start executing code. By compiling the service’s code into native code ahead of time, you can avoid this delay and start running immediately. In addition, using native code, you can create self-contained Dart Services with a small disk footprint, greatly reducing the size of the container on which Dart Services are run. Experiments show that using dart2native compiler can reduce the size of Docker image by 91%, from 220MB jit-compiled code to 20MB native compiled code! For more details on server-side applications and packages, see the following documents:

Server side application: Dart.dev /tutorials/s… Dart.dev /server/libr…


7. Problems that have not yet been solved

  • 1. Dart2.6 does not provide cross-compilation support

The dart2native compiler only supports creating native code for the operating system it runs on. Therefore, if you want to create executables for programs on macOS, Windows, and Linux, you need to run the compiler on each platform separately. One approach is to use CI (continuous integration) programs that can support all three operating systems simultaneously.

  • 2. Signatures are not supported

The resulting executable uses a format that is incompatible with standard signing tools, such as CoDesign and SignTool.

  • 3. Does not supportdart:mirrorsdart:developerThese two libraries.

8. Other functions

Dart 2.6 also includes a preview of an exciting new language feature called “static extension methods,” which allows specially declared static functions to be called as if they were instance members of a particular statically typed expression. However, this feature still needs to be worked on and will be officially launched in the next Dart SDK release.

The Dart class has virtual methods. For example, a call like thing.dostuff () will invoke the virtual doStuff method on the object that Thing represents. The only way to add methods to a class is to modify the class. If you are not the author of a class, you must use static helper functions instead of methods, so use doMyStuff(thing) instead of thing.domystuff (). This is acceptable for a single function.

More information about the static extension method can be found in the official documentation at github.com/dart-lang/l…


Reference for this article: medium.com/dartlang/da…


This article was first published on the public account Flutter. More information about Flutter is welcome.