02 Pre Study Chapter Overview of Dart Language

02 Pre-study Chapter Overview of Dart Language #

Hello, I am Chen Hang.

As we know, Flutter development framework uses the Dart programming language. Therefore, in order to make good use of this framework, we must first understand the Dart language.

When it comes to learning new technologies, I have always agreed with one viewpoint: Do not dive straight into the details. Instead, you should first get the big picture in order to understand the problem from a high-dimensional perspective. Therefore, to help you master Dart more efficiently and quickly acquire the ability to develop a Flutter application, in this article, I will first introduce the historical background, features, and future of the Dart language from the perspective of Flutter development.

Afterwards, in the “Dart Basics” section of this column, I will share in detail with you its features, basic syntax, variable types, functions, and provide you with a comprehensive case using Dart, helping you to understand and learn this language.

If you already have a preliminary impression of Dart, you can skip this preview article and directly proceed to the following content.

What is Dart? #

In October 2011, Google introduced a new programming language called Dart at the GOTO conference in Denmark. Just like Kotlin and Swift were created to solve practical problems encountered when writing applications in Java and Objective-C respectively, the birth of Dart was to address the inherent flaws in JavaScript that could not be improved at the language’s core.

Now, what are the problems and flaws of JavaScript? Brendan Eich, the father of JavaScript, once said in an interview that JavaScript was “designed in a few days”.

In summary, his design approach was as follows:

  • Borrow the basic syntax from C language
  • Borrow the data types and memory management mechanism from Java
  • Borrow the position of functions as “first-class citizens” from Scheme
  • Borrow the prototype-based inheritance mechanism from Self

Therefore, JavaScript is actually a mixture of two programming language styles: (simplified) functional programming style and (simplified) object-oriented programming style.

Because of the short design time, some details were not carefully considered, resulting in a long period during which programs developed using JavaScript were chaotic. Dissatisfied with JavaScript, Google programmers decided to create a new language to replace it, and therefore the initial positioning of Dart was as a scripting language that runs in web browsers.

In order to promote Dart, Google even embedded the Dart VM in its Chrome browser, allowing Dart code to be run directly and efficiently. For regular browsers, Google also provides a set of tools that can compile Dart code into JavaScript. This way, developers can use Dart without worrying about compatibility issues. Combined with its distinguished origin, Dart initially gained attention from some front-end developers.

However, JavaScript’s vitality turned out to be stronger than expected.

Originally, JavaScript could only run in browsers, but the emergence of Node.js enabled it to run on the server. Soon, it expanded to become the host container for mobile and desktop applications. Popular frameworks such as React, React Native, Vue, Electron, and NW (node-webkit) quickly emerged, rapidly expanding its boundaries.

As a result, JavaScript became a full-stack language that works for both front-end and back-end development, changing the development pattern of front-end and entering a new world. In the words of Atwood’s Law: “Any application that can be written in JavaScript, will eventually be written in JavaScript.”

JavaScript experienced a renaissance thanks to Node.js, but Dart did not have the same luck. Due to a lack of top-level projects using Dart, it has remained lukewarm. In 2015, after receiving feedback from numerous developers, Google decided to remove the built-in Dart VM engine from Chrome. This was a major setback for Dart’s development, and the conversation about replacing JavaScript became even more unrealistic.

However, this became an opportunity for Dart to make a transformation. It incubated the mobile development framework Flutter within Google and made a breakthrough in the field of mobile development. Moreover, Dart was designated as the official development language for Google’s future operating system, Fuchsia.

Meanwhile, the development of front-end for web browsers, which was Dart’s original domain, did not come to a halt. The well-known front-end framework Angular, in addition to its TypeScript version, also keeps iterating its corresponding Dart version, AngularDart (https://github.com/dart-lang/angular). (However, it must be mentioned that the number of stars for this project has always been a meager 1,100+).

It is precisely because of its limited user base and minimal historical baggage that Dart can completely change its mindset after going through so many stories and become a language that focuses on the large front-end and cross-platform ecosystem.

Next, let’s talk about the most important core features of Dart from the perspective of Flutter development.

Features of Dart #

Every programming language has its own characteristics, and the best language is the one that suits your needs.

As a newcomer in mobile development, Dart can be considered a compilation of the best features and influences from other excellent programming languages. Therefore, the learning curve for developers of other languages is undoubtedly low. At the same time, Dart has its own unique features that make it the perfect choice for Flutter. Thus, Dart has become the language of choice for Flutter.

Now, let me share with you the core features of Dart in detail.

JIT and AOT #

With the help of advanced toolchains and compilers, Dart is one of the few languages that support both JIT (Just In Time) and AOT (Ahead of Time) compilation. So, what are JIT and AOT?

Usually, languages require compilation before they can be executed, and JIT and AOT are the two most common compilation modes.

  • JIT compiles code dynamically at runtime and is used during development. It allows for dynamic downloading and execution of code, making development and testing efficient. However, the runtime performance and execution speed may be affected due to the overhead of just-in-time compilation.
  • AOT compiles code ahead of time, generating binary code that can be directly executed. It provides fast runtime execution and high performance, but each execution requires prior compilation, which reduces development and testing efficiency.

In summary, using JIT compilation during development can shorten the product’s development cycle. One of Flutter’s most popular features, hot reload, is based on this capability. On the other hand, using AOT compilation during release eliminates the need for inefficient method call mappings between cross-platform JavaScript code and native Android/iOS code, as seen in React Native. This is why Dart is known for its fast runtime execution and good performance.

Now, how can you determine if a language uses AOT or JIT? Typically, you can look at whether the code requires compilation before execution. If compilation is required, it is likely AOT; if not, it is likely JIT.

C/C++ are typical examples of AOT languages, as they need to be compiled into machine code before execution. On the other hand, JavaScript, Python, and almost all scripting languages are representative of JIT languages.

Memory Allocation and Garbage Collection #

Dart VM’s memory allocation strategy is relatively simple. When creating objects, it only needs to move pointers on the heap. The memory growth is always linear, eliminating the need to search for available memory.

In Dart, concurrency is achieved through Isolates. Isolates are similar to threads but do not share memory, allowing for independent execution of workers. This mechanism allows Dart to achieve lock-free, fast allocation.

Dart’s garbage collection uses a generational algorithm. In the process of reclaiming memory, the new generation adopts the “semi-space” mechanism. When garbage collection is triggered, Dart will copy the “active” objects in the current semispace to the spare space and then release all memory in the current space. During the collection process, Dart only needs to deal with a small number of “active” objects and ignores a large number of “dead” objects without references. This garbage collection mechanism is well-suited for the scenario of frequent creation and destruction of widgets in the Flutter framework.

Single-threaded Model #

Most high-level programming languages that support concurrent execution of threads (such as C++, Java, Objective-C) use preemptive thread switching. Each thread is assigned a fixed time slice for execution, and after exceeding the time slice, the thread context is preempted and switched. If the thread is updating shared resources at this time, preemption may lead to data inconsistency.

A typical solution to this problem is to use locks to protect shared resources. However, locks themselves can introduce performance overhead and more serious problems such as deadlocks.

This is where the advantage of Dart’s single-threaded model comes in, as it naturally avoids the problems of resource contention and state synchronization. This means that once a function starts executing, it will continue until the function completes without interruption by other Dart code.

Therefore, Dart does not have threads but instead has Isolates (isolated areas). Isolates do not share memory with each other and are like workers running in separate processes, communicating through an event loop and passing messages in an event queue.

No Need for Separate Declarative Layout Language #

In Flutter, interface layouts are directly defined using Dart code.

Dart’s declarative programming for layout is easy to read and visualize, eliminating the need for declarative layout languages like JSX or XML. All layouts are defined using the same format, which makes it easy to provide advanced tools to simplify layout creation.

The development process does not require a visual interface builder because hot reload allows us to immediately see the changes on the device.

The Future of Dart #

So, in this context, what will become of Dart in the future?

Dart is an excellent and young modern language, but a programming language is not considered complete just by having an engine and developer interfaces. Its true value will only begin to show when the “ecosystem” of libraries, frameworks, and applications that support the language matures. Usually, this process takes several years.

Currently, there are very few third-party libraries based on the Dart language, and their quality is average. However, it is worth celebrating that due to the promotion of Flutter and Fuchsia, the Dart SDK is being updated at a much faster pace, and developer enthusiasm is growing rapidly. Thus, Dart’s ecosystem is growing quickly.

After all, the top products in the Dart community right now are Flutter and Fuchsia. Therefore, the majority of Dart developers are Flutter developers. Additionally, there are some developers who use Dart for front-end web development, but their numbers are not significant. So, I believe the success of Dart currently depends largely on the success of Flutter and Fuchsia. Flutter, being the UI development framework for Fuchsia, determines the success of Dart as well.

As mentioned earlier, Flutter’s official version has only been released for a little over half a year, and it has already received more than 68,000 stars on GitHub, trailing behind React Native by only around 10,000. This clearly shows its popularity.

Now, let’s take a closer look at Flutter itself. Its emergence provides a complete cross-platform solution, filling the gaps in today’s cross-platform development frameworks and solving industry pain points. It has a high chance of becoming the ultimate solution in the field of cross-platform development. Its future holds great promise and is worth looking forward to.

With that, we can clearly see that after suffering from Oracle’s Java infringement case, Google, having learned from the pain, has determinedly set its vision on developing its own language ecosystem: Dart, through Flutter and Fuchsia, targeting front-end and mobile development. On the other hand, in the backend, Google has seen a rapid growth of its Go language, which has gained momentum with the popularity of Docker.

Therefore, Google’s strategy is not only comprehensive but also widely applied and influential, with killer products for both frontend and backend development to build its language ecosystem. With the release of Google’s new system, Fuchsia, I believe Flutter and Dart will unleash their power at an even faster pace, and Google’s desire to unify frontend and backend development skill sets will be partially realized.

Summary #

Today, I have introduced you to the history of Dart, explained various features of the Dart language from the perspective of a Flutter developer, and analyzed the future development of Dart.

Dart is a modern language that combines the advantages of various other languages. If you are not familiar with Dart, there is no need to worry. If you have experience in other programming languages, especially Java, JavaScript, Swift, or Objective-C, you can easily find similarities in Dart and quickly get started at a very low cost.

I hope that through this article, you can have a preliminary understanding of the Dart language and lay a solid foundation for our future learning. In the “Dart Basics” module of this column, I will compare the features of Dart with those of other programming languages and explain the similar design concepts, helping you quickly establish the necessary Dart knowledge system for building Flutter applications.

Thought Questions #

Do you have any difficulties or insights when learning Dart or other programming languages?

I welcome you to leave your experiences and perspectives in the comments section. I will be waiting for you in the next article! Thank you for reading, and feel free to share this article with more friends to read together.