01. Flutter development Dart installation and variable definition

1. Install Dart

Why do you need to install Dart?
When installing the Flutter SDK, Dart has been built in, and we can directly use Flutter to write and run Dart.
However, if you want to learn Dart alone and run your own Dart code, it is best to install a Dart SDK.

1.2. Download the Dart SDK

Official website download Official
website There are two ways to install: install through tools and download SDK directly, configure environment variables

  • Windows can be installed through tools.
    Chocolatey
    macOS can be
    installed through homebrew. The official website has detailed explanations.

  • Directly download the SDK and configure the environment variables.
    Download address: https://dart.dev/tools/sdk/archive
    After the download is complete, configure the environment variables according to the path.

1.3、VSCode

In the process of learning Dart, I use VSCode as an editor.
Using VSCode to write Dart requires installing Dart plugins: I currently have four plugins installed for this VSCode

  • Dart and Flutter plugins are prepared for Flutter development
  • Atom One Dark Theme is a theme I personally like
  • Code Runner can click the button in the upper right corner to let me quickly run the code
    insert image description here

2. Hello Dart

2.1 Hello Dart

Create a new helloWorld.dart file in VSCode and add the following content:

main(List<String> args) {
    
    
  print('Hello World');
}

Then execute dart helloWorld.dart in the terminal, and you can see the result of Hello World. VSCode has a built-in system terminal.

2.2. Analysis of the program

  1. The entry point of the Dart language is also the main function, and must be defined explicitly;
  2. Dart's entry function main has no return value;
  3. The command line parameters passed to main are done through List. It can be understood from the literal value that List is a collection type in Dart. Each of the Strings represents a parameter passed to main;
  4. When defining a string, you can use single or double quotes;
  5. Each statement must end with a semicolon, which is not required in many languages, such as Swift and JavaScript;

3. Define variables

3.1. Explicit statement

The way to declare variables clearly, the format is as follows:
variable type variable name = assignment;
sample code:

String name = 'coderwhy';
int age = 18;
double height = 1.88;
print('${name}, ${age}, ${height}'); 

Note: The defined variable can modify the value, but cannot assign other types

String content = 'Hello Dart';
content = 'Hello World'; // 正确的
content = 111; // 错误的, 将一个int值赋值给一个String变量

3.2. Type Inference

The method of type deduction to declare variables, the format is as follows:
var/dynamic/const/final variable name = assignment;

3.3.1. The use of var

Example usage of var:

  • runtimeType is used to get the current type of the variable
var name = 'coderwhy';
name = 'kobe';
print(name.runtimeType); // String

Incorrect use of var:

var age = 18;
age = 'why'; // 不可以将String赋值给一个int类型
3.3.2. The use of dynamic

If you really want to do this, you can use dynamic to declare variables:

  • But in development, dynamic is usually not used, because variables of type will bring potential danger
dynamic name = 'coderwhy';
print(name.runtimeType); // String
name = 18;
print(name.runtimeType); // int

3.3.3. The use of final&const

Both final and const are used to define constants, that is, the value cannot be modified after definition

final name = 'coderwhy';
name = 'kobe'; // 错误做法

const age = 18;
age = 20; // 错误做法

What is the difference between final and const?

  • When const is assigned, the content of the assignment must be determined during compilation
  • When final is assigned, it can be dynamically obtained, such as assigning a function
String getName() {
    
    
  return 'coderwhy';
}

main(List<String> args) {
    
    
  const name = getName(); // 错误
  final name = getName(); // 正确
}

Final and const small cases:

  • First of all, const cannot be assigned to DateTime.now()
  • Secondly, once final is assigned, it has a definite result and will not be assigned again
// const time = DateTime.now(); // 错误的赋值方式
final time = DateTime.now();
print(time); // 2021-04-05 09:02:54.052626

sleep(Duration(seconds: 2));
print(time); // 2021-04-05 09:02:54.052626

Const is placed on the right side of the assignment statement, which can share objects and improve performance:

class Person {
    
    
  const Person();
}

main(List<String> args) {
    
    
  final a = const Person();
  final b = const Person();
  print(identical(a, b)); // true

  final m = Person();
  final n = Person();
  print(identical(m, n)); // false
}

Guess you like

Origin blog.csdn.net/qq_25218777/article/details/116401162