Get the Dart language for Android and iOS at the same time (1): A preliminary study of Dart

The development of Android App and iOS App with Flutter requires the use of Dart language, so this series of articles will introduce Dart language related knowledge in depth, and a series of articles on Flutter App development will be launched in the future, so stay tuned!

1 Introduction to Dart

Dart was born on October 10, 2011. Lars Bak, the head of Google’s Dart language project, announced at the Goto conference held in Denmark that Dart is a "structured web programming language" and Dart programming language is used in all modern browsers and environments. Operate efficiently.

Although Dart is a computer programming language developed by Google, it was later recognized as a standard by ECMA. This language can be used for web, server, mobile application and Internet of Things development. It is open source software under a loose open source license (modified BSD certificate).

At present, the latest version of Dart is Dart2. Dart2 is an efficient, concise programming language that has passed a large number of practical tests, and can meet the challenges of modern application development. Dart2 has streamlined the type system, cleaned up the syntax, and rebuilt most of the development tool chain, making the development of mobile and web references more efficient.

So why did the Flutter team choose Dart? In fact, Flutter evaluated more than ten languages ​​at the beginning, and finally chose Dart, because Dart fits the way of building user interfaces very well. The following are the main features of the Dart language.

  • Dart supports AOT (Ahead Of Time) compilation method, which can compile source code into fast-executing native code. It allows almost all Flutter to be written in Dart. This not only makes Flutter run faster, but everything (including all components) can be customized.

  • Dart also supports JIT (Just in time) compilation method, the development cycle is very fast, and it can achieve sub-second hot reload.

  • Dart can create 60fps flow animation and transition more easily. . Dart can allocate objects and garbage collect without locks. And, similar to JavaScript, Dart does not use preemptive scheduling and shared memory (so no locks are needed). Since Flutter applications can be compiled into local code, the compiled program does not need to build a slow bridge (for example, JavaScript to native code) during execution. Flutter apps will also start faster.

  • Dart does not need a separate declarative layout language (such as JSX or XML), or a separate visual interface builder, because Dart's declarative layout code is easy to read and visualize. All layouts use one language, gathered in one place. Flutter easily provides advanced tools to make the layout simpler.

  • Dart language is very easy to learn because Dart has features familiar to both static language users and dynamic language users.

  • In the Dart language, everything is an object, whether it is variables, numbers, functions, etc., are all objects. All objects are instances of classes, and all classes inherit from the Object class. This is similar to the Java language: everything is an object.

  • Dart language allows you to specify the data type or not. If the data type is not specified when defining a variable, the Dart compiler will automatically infer the data type of the variable based on the value of the initialized variable on the right. If the variable is not initialized when the variable is defined, but the variable is initialized later, then the data type of the variable is dynamic. Dart language does not perform data type checking on dynamic, that is to say, variables of dynamic type can store any type of value, and can also access any properties and methods (if it does not exist, Dart language will provide special processing methods, please refer to the details) See later chapters). Even if a member of the dynamic type variable does not exist, there will be no error at compile time, but an exception may be thrown at runtime, which is similar to JavaScript. However, it is recommended to specify the data type so that the Dart compiler will detect potential errors as much as possible.

  • Dart is a single-threaded programming language, similar to JavaScript, it also supports async/await, and its usage is similar to JavaScript.

Although Dart language supports dynamic data types, Dart is not a weakly typed programming language, but a strongly typed programming language, because Dart language also supports specified data types. Let me explain what is a strong type and a weak type.

  • Strong typing (statically typed language): The return value of variables and functions needs to be compiled with a unique data type. If the data types are inconsistent, an exception will be thrown at compile time, that is, the type check fails.

  • Weak type (dynamically typed language): The data type of the variable is determined at runtime, and the data type of the variable can change at runtime. For example, a variable is of string type when it is initialized, and an integer can be assigned to the variable at runtime, then the data type of this variable is int type. This strongly typed programming language is absolutely not allowed.

The reason why Dart has the characteristics of dynamic typing is that Dart has added dynamic data types, but Dart is still a strongly typed language in essence. And when using Dart language, you should specify the data type as much as possible, which helps to allocate memory space more reasonably, improve the performance of the program, and reduce the resources occupied by the program.

Here are some evidence of strong typing in the Dart language:

(1) The data type must be specified for the variable in the code (of course, this specification can be explicit or implicit)

int number1 = 20;  // 指定number1变量的数据类型为int
var number2 = 20;   // 自动识别number2变量的数据类型为int

(2) A variable can only have one data type, and once the data type is specified, it cannot be changed.

var number = 20;
number = "hello world";  // 会抛出异常,不能将字符串类型的值赋给int类型的变量

In fact, it is now more popular to make static languages ​​dynamic, which means adding dynamic language features to static languages. In fact, Dart is not the only language that supports static language dynamics. For example, Swift, Kotlin, and Go all support static language dynamics to varying degrees. The following uses Dart, Swift, Kotlin and Go to achieve the same function, and readers can experience the benefits of this feature.

Dart language

void main() {
  var s1 = "hello world";       // 自动识别s1为字符串类型
  String s2  = "I love you.";
  print(s1);
  print(s2);
  // 输出s1的数据类型
  print( s1.runtimeType.toString());
  var s4;      // 默认值是null, s4是dynamic数据类型,因为在定义变量时未初始化
  s1 = 20;     // 抛出异常,因为不能将整数赋给一个字符串变量
}

Swift language

var s1 = "hello world"              // 自动识别s1为字符串类型
var s2:String = "I love you."
print(s1)
print(s2)
// 输出s1的数据类型
print(type(of: s1))
s1 = 20  //抛出异常,不能将整数赋给一个字符串变量

Kotlin language

var s1 = "hello world"              //自动识别s1为字符串类型
var s2:String = "I love you."
println(s1)
println(s2)
// 输出s1的数据类型
println(s1::class.simpleName)
s1 = 20  //抛出异常,不能将整数赋给一个字符串变量

Go language

s1 := "hello world"                 // 定义s1变量,并自动识别s1为字符串类型
var s2 string = "I love you."
fmt.Println(s1)
fmt.Println(s2)
fmt.Println(reflect.TypeOf(s1));
s1 = 20; //抛出异常,不能将整数赋给一个字符串变量

The following two dynamic languages ​​JavaScript and Python are used to achieve the same functions as above, and readers can experience the difference between a real dynamic language and a dynamic static language.

JavaScript language

var s1 = "hello world"              // 尽管目前s1是字符串类型,但可以改变s1的数据类型
var s2 = "I love you."
console.log(s1)
console.log(s2)
console.log(typeof(s1))
s1 = 20                            // 将s1的数据类型变为int   
console.log(typeof(s1))
s1 = true                          // 将s1的数据类型变为bool
console.log(typeof(s1))

Python language

s1 = "hello world"                  // 尽管目前s1是字符串类型,但可以改变s1的数据类型
s2 = "I love you."
print(s1)
print(s2)
print(type(s1))
s1 = 20                            // 将s1的数据类型变为int
print(type(s1))
s1 = True                          // 将s1的数据类型变为bool
print(type(s1))

 2. Install Dart SDK

Although the Dart SDK is automatically installed when installing the Flutter SDK, if you want to learn the Dart language separately, it is best to install the Dart SDK separately. This makes it easier to test and learn the Dart language.

There are usually two ways to install the Dart SDK: online installation and offline installation. The following describes the installation methods under Windows, macOS and Linux. If readers want more detailed information, they can visit Dart's official website: https://dart.dev.

2.1 Install Dart SDK on Windows

(1) Online installation

On windows, you can use chocolatey to install Dart SDK online, but first you need to install chocolatey. Readers can learn how to install chocolatey through https://chocolatey.org. After installing chocolatey, you can use the following command to install Dart SDK.

choco install dart-sdk

 

If you want to upgrade the Dart SDK, you can use the following command.

choco upgrade dart-sdk

(2) Offline installation

Readers can download the offline installation package of Dart SDK from the following page. The installation package is an exe file. After downloading, double-click to run it and install it as prompted.

http://www.gekorm.com/dart-windows

 

2.2 Install Dart SDK on macOS

 

It is much more convenient under macOS, just use the brew command to install online, the command is as follows:

brew tap dart-lang/dart

brew install dart

 

If you want to upgrade the Dart SDK, you can use the following command.

brew upgrade dart

 

 

2.3 Install Dart SDK on Linux

 

Execute the following command in the terminal to install Dart SDK.

sudo apt-get update

sudo apt-get install dart

 

Note: Under macOS and Linux platforms, the official does not provide offline installation packages, so Dart SDK can only be installed and upgraded online.

After installing the Dart SDK, enter the following command in the terminal. If the version information similar to Figure 1 can be output normally, Dart has been installed successfully.

dart --version

Figure 1 Dart SDK version information

3. Run the Dart program

After installing the Dart SDK, the first step is to check if the Dart SDK can run. You can run Dart programs directly through the command line.

First create a file named First.dart, and then enter the following code.

greet(String name) {
  return 'Hello $name';
}
main() {
  var name = "李宁";
  print(greet(name));
}

 Then execute the dart First.dart command in the terminal, and output the following string as follows, indicating that the First.dart file has been successfully run.

Hello Li Ning

4. Build Dart development environment

Although the Dart program can be run through the command line, it is generally necessary to use a suitable IDE for large-scale applications. Although the Dart language can be used in Flutter, if you start to learn the Dart language, it is recommended to learn Dart through a console-type program instead of using Flutter to develop mobile apps.

Intellij Idea is recommended here, the download address is as follows:

https://www.jetbrains.com/idea

 

Since Android Studio is developed based on the community version of Intellij Idea, the operations of Android Studio and Intellij Idea are very similar.

After installing Intellij Idea, you need to configure Dart SDK first. Under macOS, open the Intellij Idea preferences dialog box, find Dart in the list tree on the left, and configure it as shown in Figure 2 on the right.

Figure 2 Configure Dart SDK under macOS

Under Windows, you need to open the Settings dialog box of Intellij Idea, as shown in Figure 3. The setting method is similar to that under macOS.

Figure 3 Configure Dart SDK under Windows

After setting up the Dart SDK, click the File> New> Project menu item in Intellij Idea, the New Project dialog box shown in Figure 4 will pop up, click Dart in the left list, and then click the Next button in the lower right corner .

Figure 4 Create Dart project

After entering the next page, you will see the page shown in Figure 5, where you can set the name and project directory of the Dart project. Then click the Finish button to create a new Dart project.

Figure 5 Specify the project name and project directory

In the Dart project tree, you can create a subdirectory to save Dart source code files, then click the subdirectory, and click the Dart File menu item in the right-click menu as shown in Figure 6.

Figure 6 Right-click pop-up menu

After clicking the Dart File menu item, the New Dart File dialog box shown in Figure 7 will pop up, and enter a character string as the file name (no need to specify the file extension). Then click the OK button to create a new Dart file.

Figure 7 Create Dart file

Then you can enter the code of the Frist.dart file written in the previous section into the First.dart file of Intellij Idea, finally click the First.dart file, and click the Run'First.dart' menu item in the right-click menu to run First. dart, the running result is shown in Figure 8.

Figure 8 Write and run Dart code in Intellij Idea

5. Test Dart program online

If readers just want to test the Dart program temporarily, they can use the official DartPad. The website is as follows:

https://dartpad.dev

 

Enter the Dart source code on the left side of the page, and then click the Run button, the running result will be output on the right side, as shown in Figure 9.

Figure 9 Run Dart program online

If you are interested in this article, you can add teacher Li Ning's WeChat public account (unitymarvel):

Follow the official account of Geek Origin to get more free technical videos and articles.

Guess you like

Origin blog.csdn.net/nokiaguy/article/details/107724784