Why Dart is used for Flutter? This topic, like PHP is the best language in the world, the debate has never stopped, there are many arguments, such as:

  • Google is promoting Dart, who is a biological son.
  • The Flutter team and the Dart team are adjacent to each other, making it easier to communicate.

There may be such considerations, but even for a real son, mud needs to be able to support the wall, do you really think such a big decision depends on these subjective colors? Not likely, so Dart must have a huge advantage.

preface

More than 10 languages were evaluated by the Flutter team at its inception. The Flutter team evaluated from four main dimensions, taking into account the needs of framework authors, developers, and end users. The Dart scored high on all of the evaluation dimensions and met all requirements and criteria.

Dart earned high marks on the following key criteria:

  • Developer productivity. One of Flutter’s main value propositions is that it allows developers to create apps for iOS and Android using the same code base, saving engineering resources. Using an efficient language can further accelerate developers and make Flutter more attractive. This is very important for both our framework team and our developers. Most of the languages of Flutter are built in the same language that we provide to our users, so we needed to keep 100K lines of code productive without sacrificing the accessibility or readability of the developer’s framework and widgets.
  • object-oriented. For Flutter, we need a language that fits the domain of Flutter problems: creating a visual user experience. The industry has decades of experience building user interface frameworks using object-oriented languages. Although we could use a non-object-oriented language, that would mean reinventing the wheel to solve some of the hard problems. In addition, most developers have experience with object oriented development, which makes learning the Flutter development method much easier.
  • Predictable high performance. With Flutter, we want to enable developers to create a fast, fluid user experience. To do this, we need to be able to run a lot of final developer code in each animation frame. This means that we need a language that provides both high performance and predictable performance without periodic pauses that result in frame loss.
  • Rapid distribution. The Flutter framework uses a functional-style process that relies heavily on the underlying memory allocator to efficiently handle transient short-term allocations. This style was developed using a language with this property and does not work effectively in languages that lack this feature.

Dart

Here is a list of Dart features that are essential to Flutter:

  • Dart is compiled by AOT (Ahead Of Time) into fast, predictable native code. Almost all Of Flutter can be written in Dart. Not only does this make Flutter faster, but almost everything (including all the widgets) is customizable.
  • Dart can also be compiled with JIT (Just In Time) to enable exceptionally fast development cycles and game-changing workflows (including Flutter sub-second stateful hot reload).
  • Dart makes it easier to create smooth animations and transitions that run at 60fps. Dart can do object allocation and garbage collection without locking. Like JavaScript, Dart avoids preemptive scheduling and shared memory (and thus locking). Because Flutter applications are compiled to native code, they do not need to build slow Bridges between domains (for example, between JavaScript and native). They also start up much faster.
  • Dart allows Flutter to avoid declarative layout languages like JSX or XML and does not require a separate visual interface builder because Dart’s declarative programmatic layout is easy to read and visualize. Also, because all layouts are displayed in one language and one location, Flutter can easily provide advanced tools to make layouts easy.
  • Developers find Dart particularly easy to learn because it has features familiar to users of both static and dynamic languages.

Not all of these features are unique to Dart, but their combination reaches a sweet spot that makes Dart unique in implementing Flutter. With so much, it’s hard to imagine Flutter being as powerful as it is without Dart.

Compilation and Execution

If you already know about static and dynamic languages, AOT and JIT compilation, and virtual machines, you may want to skip this section.

Historically, computer languages have been divided into two categories: static languages (for example, Fortran or C, where variables are statically typed at compile time) and dynamic languages (for example, Smalltalk or JavaScript, where variables’ types can change at run time). Static languages are typically compiled to generate native code (or assembly code) programs of the target computer that are executed directly by the hardware at run time. Dynamic languages are executed by the interpreter without producing machine language code.

Then, of course, things got more complicated. The concept of a virtual machine (VM), which is really just a high-level interpreter that mimics a hardware machine in software, became popular. Virtual machines make it easier to port languages to new hardware platforms. In this case, the input language of the VM is usually an intermediate language. For example, a programming language (such as Java) is compiled into an intermediate language (bytecode) and then executed on a VM (JVM).

In addition, there are just-in-time (JIT) compilers. A JIT compiler runs during program execution and compiles on the fly. The original compiler that executes during program creation (before runtime) is now called an AOT compiler.

In general, only static languages are suitable for compiling AOT into native machine code, because machine languages usually need to know the type of data, whereas in dynamic languages, the type is not fixed in advance. Therefore, dynamic languages are usually interpreted or jIts compiled.

AOT compilation during development, the development cycle (the time from changing the program to being able to execute the program to see the results of the changes) is always slow. But AOT compilation produces programs that perform more predictably and run without stopping to analyze and compile. Aot-compiled programs also start executing faster (because they’ve already been compiled).

JIT compilation, on the other hand, provides a faster development cycle but results in slower or more erratic execution. In particular, JIT compilers have slower startup times because the JIT compiler must analyze and compile the code before executing it when the program starts to run. Studies have shown that many people will abandon the application if it takes a few seconds to get started.

That’s all about AOT and JIT. Wouldn’t it be great to combine the benefits of AOT and JIT compilation? Dart provides significant benefits by supporting both AOT and JIT compilation.

Compile and execute Dart

Prior to working on Dart, members of the Dart team had done pioneering work on advanced compilers and virtual machines, both for dynamic languages (such as the V8 engine for JavaScript, Strongtalk for Smalltalk) and static languages (such as the Hotspot compiler for Java). They use that experience to make Dart incredibly flexible in how it is compiled and executed.

Dart is one of the few languages suitable for compiling BOTH AOT and JIT. Supporting both compilations provides significant advantages for Dart and (especially Flutter).

JIT compilation is used during development, using a compiler that is exceptionally fast. Then, when the application is ready for release, it is AOT compiled. Therefore, with the help of advanced tools and compilers, provide the best of both worlds: extremely fast development cycles and fast execution and startup times.

Dart’s flexibility in compilation and execution doesn’t stop there. Dart, for example, can be compiled to JavaScript so that the browser can execute it. This allows code to be reused between mobile and Web applications. Developers report code reuse rates as high as 70 percent between their mobile and Web applications. Dart can also be used on a server by compiling it into machine code, or by compiling it into JavaScript and using it with Node.js.

Finally, Dart also provides a standalone VM that uses the Dart language itself as an intermediate language (essentially acting as an interpreter).

Dart effectively compiles AOT or JIT, interprets or converts to other languages. Dart compilation and execution are not only extremely flexible, but also extremely fast.

Stateful thermal overload

One of the most popular features of the Flutter is its extremely fast thermal overload. During development, Flutter uses a JIT compiler, which can normally reload and continue executing code within a second. Whenever possible, the state of the application remains the same with each reload, so the application can continue running from the break.

Unless you’ve experienced it yourself, it’s hard to understand how important really fast (and reliable) hot reloading is in the development process. The thermal loading feature of Flutter makes it much easier to try out new ideas or alternative approaches, greatly promoting creativity.

Here’s what a mobile app developer had to say about Flutter thermal overload:

I wanted to test hot overloading, so I changed the color, saved the changes, and… I just love it ❤!

This feature is really great. I thought Edit & Continue worked fine in Visual Studio, but this is amazing. With this feature, I think mobile developers can be twice as productive.

It was a real sea change for me. When I deploy the code and it takes a long time, I get distracted and do other things, and when I get back to the simulator/device, I forget what I want to test. What could be more frustrating than spending 5 minutes moving a control 2px? With Flutter, this no longer exists.

Avoid caton

A fast app is great, but a smooth app is even better. Even super fast animations can look bad if they look awkward. But preventing congestion can be difficult because there are so many factors. Dart has a number of features to avoid many common causes of stutter.

Of course, (like any language) it is still possible to write a crude application in Flutter. Dart is more predictable and helps developers better control the smoothness of their applications, making it easier to provide the best user experience and do anything.

The user interface created by Flutter runs at a speed of 60 FPS, far outperforming user interfaces created by other cross-platform development frameworks.

Not only better than cross-platform applications, but just as good as the best native applications:

The user interface is very smooth… I’ve never seen such a smooth Android app.

AOT Assembly and “Bridges”

One feature we discussed that helps keep things smooth is that Dart is aoT-compiled to native machine code. Pre-compiled AOT code is more predictable than JIT because there is no need to pause JIT analysis or compilation at run time.

Aot-compiled code, however, has the even greater advantage of avoiding “JavaScript Bridges.” When dynamic languages (such as JavaScript) need to interoperate with native code on the platform, they must communicate over the bridge, which results in a context switch that must save a lot of state (possibly to secondary storage). These context switches are a double whammy because they not only slow things down but can cause serious problems.

Note: Even compiled code may require an interface to talk to platform code, which can also be called bridging, but is usually orders of magnitude faster than bridging required for dynamic languages. In addition, Dart reduces the need to cross Bridges because it allows things like widgets to be moved into the application.

Preemptive scheduling, time sharding and shared resources

Most computer languages that support multiple concurrent threads of execution (including Java, Kotlin, Objective-C, and Swift) use preempt to switch between threads. Each thread is assigned an “execution time,” and if the allotted time is exceeded, a context switch is used to preempt the thread. However, if preemption occurs when updating a resource shared between threads, such as memory, this can result in a contention condition.

Contending conditions are a double whammy because they can cause serious errors, including crashing the application and causing data loss, and are particularly difficult to find and fix because they depend on the relative time of independent threads. It is not uncommon for race conditions to stop showing up when you run an application in the debugger.

The typical way to resolve contention conditions is to protect shared resources with locks that prevent other threads from executing, but locks themselves can lead to tricky or even worse problems (including deadlocks and starvation).

Dart takes a different approach to this problem. Threads in Dart, called isolates, do not share memory, thus avoiding most locks. Isolate communicates by passing messages over channels, similar to actors in Erlang or Web workers in JavaScript.

Dart, like JavaScript, is single-threaded, which means it doesn’t allow preemption at all. Instead, threads are generated explicitly (using async/await, Futures, or Streams). This gives the developer more control over execution. Single-threading helps developers ensure that critical functions, including animations and transitions, are performed without preemption. This is usually a big advantage not only for the user interface, but also for other client-server code.

Of course, if the developer forgets to relinquish control, this can delay the execution of other code. However, we find that forgetting to yield is usually much easier than forgetting to lock (because it’s hard to find competition conditions).

Object allocation and garbage collection

Another serious cause of garbage is garbage collection. Indeed, this is just a special case of accessing a shared resource (memory), which in many languages requires locking. But locks can prevent the entire application from running while the available memory is being collected. However, Dart can perform garbage collection almost all the time without locking.

Dart uses an advanced generational garbage collection and object allocation scheme that is fast for allocating many short-term objects (perfect for a reactive user interface like Flutter, which reconstructs an immutable view tree for each frame). Dart can allocate an object with a pointer bump (no locking required). This results in smooth scrolling and animation without stuttering.

Unified layout

Another benefit of Dart is that Flutter does not split layouts between your program and other templatation or layout languages (such as JSX or XML), nor does it require a separate visual layout tool. Here is a simple Flutter view written in Dart:

Center(child:
  Column(children: [
    Text('Hello, World!'),
    Icon(Icons.star, color: Colors.green),
  ])
)
Copy the code

But I know what you’re probably thinking – how can the lack of a specialized layout language be an advantage? But it is subversive. This is what the developers wrote in an article titled “Why Native Application Developers should Take Flutter Seriously”.

In Flutter, the interface layout is defined directly through Dart coding. There is no need to use XML or template languages. There is no need to use tools such as visual designer.

My hunch is that some of you may even cringe a little after hearing this news. On the surface, that’s my reaction, too. Isn’t it easy to use visual tools for layout? Does coding all the constraint logic make things too complicated?

It didn’t. Oh, my god, it just opened my eyes.

The first part of the answer is the thermal overload mentioned above.

That’s years ahead of Android’s Instant Run and any similar solution. The same applies to large applications. That speed is the Dart’s strength.

In effect, the visual editor becomes redundant. I don’t miss XCode’s auto-layout at all.

Dart creates a layout that is clean and easy to understand, while “super-fast” thermal reloads provide immediate results. This includes the non-static parts of the layout.

As a result, layouts in Flutter are much faster than in Android/XCode. Once you get the hang of it (which took me a few weeks), you can save a lot of overhead because there are very few context switches. You don’t have to switch to design mode, select the mouse and start clicking, then wonder if something has to be done programmatically, how to do it, etc. Because everything is programmed. And these apis are very well designed. It’s intuitive and more powerful than automatic layout XML.

For example, here is a simple list layout with dividers (horizontal lines) added between all other items defined programmatically:

return new ListView.builder(itemBuilder: (context, i) {
  if (i.isOdd) return new Divider(); 
  // rest of function
});
Copy the code

In Flutter, all layouts exist in the same location, whether static or programmatic. The new Dart tools, including the Flutter Inspector and the outline view (with all layout definitions in code) make complex and aesthetically pleasing layouts much easier.

Is Dart a proprietary language?

No, Dart (i.e., Flutter) is fully open source, clearly licensed, and ECMA compliant. Dart is popular inside and outside Google. It is one of the fastest growing languages within Google and is used by Adwords, Flutter, Fuchsia and other products; Outside Of Google, the Dart code base has more than 100 external submitters.

A better indicator of Dart’s openness is the growth of the community outside Of Google. For example, we’ve seen a steady stream of Dart articles and videos from third parties (including Flutter and AngularDart), some of which I’ve referenced in this article.

In addition to external subcommitters of Dart itself, the common Dart package repository contains more than 3,000 packages, including libraries for Firebase, Redux, RxDart, Internationalization, encryption, databases, routing, collections, and more.

Will Dart programmers be easy to find?

Is it harder to find qualified programmers if there aren’t many programmers who know Dart? Of course not. Dart makes it easier to find programmers because it’s a very fast and easy-to-learn language. Programmers in languages like Java, JavaScript, Kotlin, C#, or Swift can start programming in Dart almost immediately. Most importantly, hot reloading encourages users to play with Dart and try new things, which makes learning Dart faster and more enjoyable.

This is what one programmer said in an article titled “Why Flutter will Take off in 2018” :

Dart is the language for developing Flutter applications and is ** easy to learn. Google has experience in creating simple, well-documented languages such as Go. So far, Dart reminds me of Ruby for me, and it’s been a joy to learn. It is not only suitable for mobile devices, but also for Web development.

Another article on Flutter and Dart, titled “Why Flutter? Instead of frame X, right? Even better, why should I go all out.”

Flutter uses the Dart language created by Google. To be honest, I don’t like strongly typed languages like C# or JAVA, but I don’t know why Dart looks different in the way it codes. I feel comfortable writing. Maybe it’s because it’s so easy to learn.

Through extensive UX research and testing, Dart is designed to be familiar and easy to learn. For example, in the first half of 2017, the Flutter team conducted UX research with eight developers. We gave them a brief introduction to Flutter and then loosened them up for an hour or so to create a simple view. All participants were able to start programming immediately, even if they had never used Dart before. They focus on writing responsive views, not language. The Dart is ready to go.

Finally, one of the participants (who made particularly great progress on the task) didn’t mention the language, so we asked them if they were aware of which language they were using. They don’t know. Language doesn’t matter; They can program in Dart in a matter of minutes.

The hard part of learning a new system is often not learning the language, but learning all the libraries, frameworks, tools, patterns, and best practices that have been written. The Dart libraries and tools are excellent and well documented. “As a bonus, they also take great care of their code base and have some of the best documentation I’ve ever seen,” one article declared. Learning Dart takes very little effort to make up for by saving the rest of the study time.

As direct proof, a large project within Google is looking to port its mobile applications to iOS. They were going to hire some iOS programmers, but decided to try Flutter. They monitored how long it took developers to quickly master Flutter. Their results showed that programmers could learn Dart and Flutter and improve their productivity in three weeks. By contrast, the five weeks they had previously observed could get programmers up and running on Android alone (not to mention the fact that they had to hire and train iOS developers).

Finally, the article “Why We chose Flutter and How it can Make our company better” comes from a company that migrates large enterprise applications to Dart on all three platforms (iOS, Android and Web). Their conclusion:

Recruiting has become much easier. Whether they’re from the Web, iOS, or Android, we now want to accept the best candidates.

Now we have 3x productivity because all our teams are focused on one code base.

Knowledge sharing reached an unprecedented height.

Using Dart and Flutter, they were able to increase productivity threefold. Given what they’ve done before, that’s not surprising. Like many companies, they are building separate applications for each platform (Web, iOS, and Android) using separate languages, tools, and programmers. Switching to Dart means they no longer need to hire three different programmers. It’s easy for them to move existing programmers to Dart.

They and others have found that once programmers start using Flutter, they often fall in love with Dart. They like the simplicity and lack of ritual. They like language features such as cascading, named parameters, async/wait, and streaming. Most importantly, they love the Flutter capabilities that Dart provides (such as thermal overloading), and Dart helps them build beautiful, high-performance applications.

The secret is focus.

The improvements in Dart 2 focus on optimizing client development. But Dart is still a great language for building server-side, desktop, embedded systems or other programs.

Focus is a good thing. A focus can benefit almost any enduring popular language. Such as:

  • C is a system programming language for writing operating systems and compilers. It becomes more.
  • Java is a language designed for embedded systems.
  • JavaScript is the scripting language for Web browsers (!) .
  • Even the much-hated PHP succeeded because of its focus on writing (and calling it) “personal home pages.”

On the other hand, many languages have explicitly tried (and failed) to become fully general-purpose, such as PL / 1 and Ada. The most common problem is that, without concentration, these languages become the proverbial kitchen sink.

Many of the features that make Dart a great client language also make it a better language to use on the server side. For example, Dart avoids the fact that pre-emptive multitasking gives it the same advantages as Node on the server, but typing is better and more secure.

The same goes for writing software for embedded systems. Dart’s ability to reliably handle multiple concurrent inputs is key.

Finally, Dart’s success on the client will inevitably lead to more interest in using it on the server – just as happened with JavaScript and Node. Why force people to build client-server software in two different languages?

conclusion

This is an exciting time for Dart. People who use Dart love it, and the new features in Dart 2 make it an even more valuable addition to your toolkit. If you are not using Dart, hopefully this article has provided you with valuable information about what is new or different about Dart and how you can try it with Flutter.

Check the English original: https://hackernoon.com/why-flutter-uses-dart-dd635a054ebf

communication

Old Meng Flutter blog address (nearly 200 controls usage) : laomengit.com