Flutter Tutorial (2) Understanding Dart Language

In the previous tutorial, I said that Flutter is developed in the Dart language. This tutorial introduces the Dart language as a whole. Note that this article will not introduce the specific syntax details of Dart, nor will it introduce Dart VM and dart2js, because these have nothing to do with Flutter. This article will introduce the characteristics, ecology and future of Dart language from the perspective of Flutter development.

Note: If you want to quickly understand Dart for Flutter development, Flutter Chinese website has a list of Dart language resources , read it in order, I believe you can get started directly.

Dart language features

First, let's take a look at why Flutter chooses Dart as the development language. The following are the official reasons:

Flutter was evaluated on four main dimensions, taking into account factors such as the needs of framework authors, developers, and end users. We found that different languages ​​met some of the requirements at different levels, but Dart scored highly on all evaluation dimensions and met all of our requirements and criteria.

The Dart runtime and compiler support a combination of two of Flutter's key features: a rapid JIT-based development cycle : a language that allows for shape changes and stateful hot reloading using typed languages; and an AOT compiler that generates efficient ARM code , you can start quickly and have predictable production deployment performance.

Additionally, we have the opportunity to work closely with the Dart community, which is actively investing resources to improve the use of Dart in Flutter. For example, when we adopted Dart, the language did not provide a toolchain to generate native binaries (which is great for achieving predictable high performance), but it is now possible because the Dart team built it for Flutter. Again, Dart VM has been optimized for throughput before, but the team is now optimizing VM latency, which is more important for Flutter workloads.

Dart scored highly on the following key criteria:

  • Developer efficiency . One of Flutter's main value propositions is the savings in engineering resources by letting developers use the same codebase to create apps for both iOS and Android. Using an efficient language can further speed up the development cycle and make Flutter more attractive. This is very important to both our framework team and developers. Most of the Flutter functionality is implemented in Dart, so we need to be efficient at 100,000 lines of code without sacrificing the readability of the framework and widgets.
  • Object Oriented . While we could use a non-object-oriented language, that would mean re-solving several hard problems. Also, the vast majority of developers have experience with object-oriented development, making it easier to learn how to develop with Flutter.
  • Predictable, high performance . With Flutter, we want to enable developers to quickly create smooth user experiences. In order to achieve this, we need to be able to run a lot of code in each animation frame. This means we need a language that delivers both high performance and predictable performance without periodic pauses that can cause dropped frames.
  • Fast memory allocation . The Flutter framework uses functional streams, which rely heavily on the underlying memory allocator, so efficiently handling small, short-lived memory allocations can be very important, so Flutter cannot work efficiently in languages ​​that lack this feature.

To sum up, the main information is that Dart does not have a VM in flutter, it can be directly compiled into local code through the AOT compiler, and memory management is more suitable for Flutter. The above is the official statement, here are some personal opinions: From the author's opinion on Dart In terms of usage, Dart's design goal should be to target both Java and Javascript.

Dart vs Java

Let’s take a look at Dart and Java first. Objectively speaking, Dart’s syntax level is indeed more expressive than Java. At the VM level, Dart VM has been optimized for memory recovery and throughput, but the specific performance comparison, the author did not find relevant Test data, but in my opinion as long as the Dart language can be popular, the performance of vm need not worry, after all, Google already has go (without vm but with GC), javascript (v8), dalvik (java vm on android) After a lot of accumulation, it is worth noting that Dart can already achieve gc within 10ms in flutter, so compared with Java, Dart will not be the decisive factor in performance, but in terms of syntax, Dart is more expressive than java , the most important thing is that Dart's support for functional programming is much stronger than that of java (currently only staying in lamda expressions), and Dart's lack of ecology is discussed below.

Dart vs JavaScript

JavaScript's weak typing has always been caught short, so there is a market for typeScript, coffeescript and even Facebook's flow (although not a superset of JavaScript, but also provides static type checking through annotation and packaging tools). In fact, in the author's opinion, the most powerful and dynamic scripting language at present is javascript, which supports dynamic expansion of attributes, creation of functions, etc., which is super cool to use. But you may have heard another voice. The powerful dynamic of javascript is a double-edged sword. After all, some people are always worried about the code they write, and they expect a static type checking system to help them reduce errors. Dart There is static type checking (dynamic type checking was added after Dart2.0, which has been enabled in the latest version of flutter), and dart code can be converted into js code through dart2js. At this level, Dart and typeScript, Coffeescript is similar, so dart does not have any obvious advantages over typescript and coffeescript in isolation, but in general, dart can perform server-side scripting, APP development, and web development, which has advantages.

Dart in Flutter

Flutter is able to run most dart code that does not directly or indirectly import dart:mirrors or dart:html. The dart code in Flutter will be compiled into local code by the AOT compiler, there is no vm, and the dart team has optimized it specifically for flutter.

Dart Ecology

The success or failure of a language depends on ecology. The quality of the ecology has two main indicators: the number of third-party libraries and the number of developers.

Dart currently has few third-party libraries, and the quality is not high. Even Dart SDK has many bugs. The number of issues opened on dart-sdk github has been maintained at around 5000. In the past 10 days, I mentioned a P0 bug And multiple features, it can be seen that Dart is still not very perfect. But fortunately, the current Dart update iteration is much faster, which is driven by flutter and Fuchsia.

As for developers, most dart users are flutter users at present, and there should be very few server-side scripts written in dart. After all, there is no well-known or time-tested web server framework in the dart community. From the dart-related questions on stackoverflow, there are some developers who use dart to develop js, but it is estimated that it will not exceed 20% of dart developers.

Flutter Chinese Community Contribution

Flutter Chinese website launched an open source project plan , aiming to develop a series of commonly used (practical) packages and plug-ins in addition to the Flutter SDK to enrich the third-party libraries of Flutter and contribute to the Flutter ecosystem from Chinese developers. Currently, the community has Several open source projects have started public testing. For details, please check:  Flutter Chinese Network Open Source Project (ps: Currently , the Flutter Chinese Network Open Source Project Team is looking for developers with strength, dreams, and keen to share to join. If you are interested, click to enter, for contact details).

Dart Future

Although the Dart ecology is not yet perfect, it is gratifying that the growth rate is very fast. More than 5 new packages are released on the pub every day. You may not agree with this number, but in fact, the most commonly used packages in java are only 200. about. Although the Dart language itself has its own bright spots, there are currently mature and good solutions in its target field, so I think whether Dart is successful or not depends on whether flutter and Fuchsia can succeed.

In the absence of the first official version of Flutter, there are currently nearly 23K stars on Github, and the popularity is quite high (of course, it cannot be compared with front-end web frameworks such as vue, which have to go up to 100k. The scale of the developer groups is different. Open source time varies). From the perspective of Flutter itself, the performance of the developed application is indeed much smoother, and it has a significant performance improvement over weex and rn.

Also, with the release of Google Fuchsia OS, Dart and flutter become first-class citizens, which means that Dart will become the main development language for devices with Fuchsia OS in the future.

finally

Some resources:

  1. For Flutter resource tutorials, please visit the Flutter Chinese website .
  2. Flutter http request library dio .
  3. Flutter Tutorial

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325377146&siteId=291194637