1
0

dart tutorial1

2026-05-30
2026-05-30

Dart Development Tools

We use vscode

1. Find the VSCode plugin to install Dart

2. Find the Code Runner extension in VSCode and install it. Code Runner can run our files.

Create a new Dart example file

Dart files end with.dart

Example:

main {
    print('hello dart');
}

Right click on "run code" in VSCode to execute

Introduction to Dart Entry Method

The entry method of Dart is the main method,

After each sentence in Dart, a semicolon should be added at the end;

Adding void before main indicates that the main method has no return value

Dart Print

dart annotation

Method 1, Backslash Comment

Dart can be commented using forward slashes, and you can select multiple lines for batch commenting by pressing Ctrl+/ in VSCode

Method 2, /*... */ multi-line comment

/*
注释内容
...
 */

Method 3: /// Three slashes for comments

///这也是一个注释

dart variable

In Dart, a variable can be declared using the var keyword

For example:

var str = 'this is var';

Of course, it can also be declared by specifying the type of the variable:

String str = 'this is var';

Dart is a powerful scripting programming language that does not require pre-defining variable types and can automatically perform type inference

Note: var and the specified type cannot be used simultaneously. If you write var, do not write the type; if you write the type, do not write var. Writing both var and the corresponding type will result in an error.

Example:

var str = '你好dart';
var myNum = 1234;
print(str);
print(myNum);


//字符串
String str = '你好dart';
print(str);

//数字类型
int myNum = 123456;
print(myNum);

//dart 类型校验
var str = '';//str被推断为String类型
str=123;//报错,str为String类型

Naming Rules

1. Variable names must consist of numbers, letters, underscores, and dollar signs ($)

2, Note that the identifier cannot start with a number

3. Identifiers cannot be reserved words or keywords

Variable names are case-sensitive: age and Age are different variables. In actual use, it is also recommended not to use a

Recommended identifiers (variable names) must be self-explanatory

Example:

var str1 = '1234';
var 2str = '123';//错误,变量名字不能以数字开头
var if = '456';//错误,if是关键字


//age和Age是不同的变量,变量区分大小写,但不同的变量不建议同名
var age = 20;
var Age = 30;
print(age);
print(Age);

//变量要见名知意
var price = 12;
var name = 1234;//用name表示价格语法不会出错,但是语义早造成程序很难理解

Constant

Dart Constants: final and const Modifiers

The value of const remains unchanged and must be assigned at the beginning

final can start without being assigned a value and can only be assigned once; while final not only has the characteristics of compile-time constants like const, but most importantly, it is a runtime

For quantities that never change, use final and const modifiers instead of var or other variable types.

Example

final name = 'Bob';//Without a type annotation
final String nickname = 'Bobby';
const bar = 1000000;// Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar;//standard atmosphere

Example of Comparison with Variables

Variable:

var str = 'this is a str';
print(str);
str = '你好,str';
print(str);//变量可以修改

int myNum = 1234;
myNum = 4567;
print(myNum);

Constant

//const定义常量
const PI = 3.1415926;
PI = 123;//错误,常量不可以修改
print(PI);


//final定义常量

final PI = 3.14159;
PI = 123;//错误,常量不可以修改
print(PI);

final a = new DateTime.now();
print(a);


//final 可以开始不赋值,但只能赋一次,final不仅有const编译时常量的特性,
//最重要的是它是运行时的常量,并且final是惰性初始化,也就是在运行时第一次使用前才初始化
const a = new DateTime.now();//错误

dart data types

The following data types are supported in Dart:

Common data types:

Numbers: int, double

Strings: String

Booleans: bool

List (Array): In Dart, arrays are list objects, so most people simply refer to them as lists

Maps (Dictionary): Generally speaking, a Map is an object related to Attribute–Value Pairs, where both the key and value can be objects of any type,

Types not commonly used in the project: Runes, Symbols

String Type

1. Several Ways to Define Strings

var defines a string

//可以用单引号,也可以用双引号,
//但是注意,引号是成对出现的,字符串值的内容包括在引号内
var str1 = 'this is str1';
var str2 = "this is str2";
print(str1);
print(str2);

The String keyword defines a string. If the type is determined, it is still recommended that you use the specified type for definition

String str1 = 'this is str1';
String str2 = 'this is str2';

print(str1);
print(str2);

Define string types using three single quotes or three double quotes

//三个引号成对出现
//三个引号和一个或者两个引号的区别:三个引号可以换行定义字符串的值
String str1 = '''this is str1''';

//这样写会报错
String str1 = 'this is
str1';

2, String Concatenation

String str1 = "你好";
String str2 = "Dart";
print("$str1 $str2");

//使用 + 号拼接
print(str1 + str2);
print(str1 + " " + str2);

Value Type

There are two numeric types in Dart, one is int and the other is double, where int represents integer type and double represents floating-point type

1, int type

int a= 123;
print(a);

a=12.2;//报错

2, double type

double b = 23.5;
print(b);
b= 24;//正确

3, Operator + - * / %

var c = a+b;
print(c);

Boolean Type

Defined using the bool keyword, boolean values include two: true and false;

Example

bool flag = true;
print(flag);
bool flag1 = false;
print(flag1);
bool flag2 = 123;//错误

Conditional Judgment

var flag = true;
if(flag){
    print('真');
}else {
    print('假');    
}


var a = 123;//赋值语句
var b = 456;

//两个等号表示判断两个值是否相等
if(a==b){
    print('a等于b');
}else {
    print('a不等于b');
}

By default, Dart does not automatically convert our types

var a = 123;
var b = '123';

if(a==b){
    print('a等于b');
}else {
    print('a不等于b');
}

//结果是a不等于b

List Type

The list type is a collection type in Dart, or also known as an array type

A list can store a series of data, and the data type of each element in the list can be different

1. The first way to define a list

var l1 = ["张三",20,true];
print(l1);

//获取集合的长度
print(l1.length);

//获取集合中的某个元素
print(l1[0]);//获取集合中的第一个元素,元素下表从0开始

2, the second way to define a list, specifying the type

var l2 =  <String>['张三','李四'];//元素必须为指定类型
var l3 = <String> ['张三',4];//报错,4是数值类型

var l4 = <int> [1,2,3,4];//int类型集合
print(l4);

3, the third way to define a list, adding data

var l5 = [];//通过中括号创建的集合,它的长度是可以变化的
print(l5);
print(l5.length);
l5.add("张三");
l5.add("李四");
l5.add(20);

print(l5);
print(l5.length);


var l6 = ["张三",20,true];
l6.add(40);//正确,只要是通过中括号创建的集合,都可以添加元素

4, the 4th way to create a list

var l7 = new List();//在新版本已废弃,运行报错
var l8 = List.filled(2," ");//创建固定长度的集合 参数1:集合长度,参数2:填充内容
print(l8);

//修改元素内容
l8[0]= "张三";
l8[1] = "李四";
print(l8);
l8.add("王五");//报错
l8.length = 0;//报错,不可修改长度

Can the length of an array created using square brackets be modified?

var l9 = [1,2,3,4];
print(l9.length);
l9.length = 0;//正确,可以改变长度
print(l9);

Fixed-length array specified type

var l10 = List<String>.filled(4,"");//集合数据必须是String类型
var l11 = List<int>.filled(4,1);//集合数据必须是int类型

l10[0] = "张三";//l10修改的类型必须是指定的String类型
l10[1] = 4;//报错

//如果List.filled没有指定元素数据类型,那么元素的类型会根据填充的值进行自动推导。

map type (dictionary type)

is somewhat similar to HashMap in Java and also resembles JSON in JavaScript

1. The first way to define a map

var person = {
    "name":"张三",
    "age":20,
    "work":["程序员","滴滴"];
};

print(person);

//获取key对应的value

print(person["name"]);//打印获取到的name的值
print(person["age"]);//打印获取到的age的值

2. Create a map using the new method

var p = new Map();
p["name"] = "李四";
p["age"] = 24;

print(p["name"]);
print(p["age"]);

Type Judgment

In Dart, the type can be determined using the is keyword

var str = '1234';

if(str is String){
    print("str 是 String 类型");
}else if(str is int) {
    print("str 是 int 类型");
}

Dart Operators

Arithmetic Operators

+

Addition

-

Subtract

*

Multiply

/

Divide

~/

Round

%

Modulo

int a = 13;
int b = 5;

print(a+b);//加
print(a-b);//减
print(a*b);//乘
print(a/b);//除
print(a%b);//取余
print(a~/b);//取整

var c = a*b;
print(c);

Relational Operator

==

Equal

!=

not equal

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

int a = 5;
int b = 3;

print(a==b);
print(a!=b);
print(a>b);
print(a<b);
print(a>=b);
print(a<=b);

if(a>b){
    print("a大于b");
}else {
    print("小于等于b");
}

Logical Operators

!

Negate

&&

and

||

or


//取反  !
bool flag = true;
print(!flag);

//并且 &&  两侧变量全部为true的话表达式值为true,否则为false;
bool a = true;
bool b = false;
print(a&&b);


//或   两侧变量有一个为true结果就为true
bool c = true;
bool d = false;
print(c||d);



Example

//如果一个人的年龄是20,并且性别是女的话,我们打印出这个人的信息

int age = 30;
String sex = "女";

if(age == 20 && sex == "女"){
    print("$age --- $sex");
}else{
    print("不打印");
}

Assignment Operator

=

Assignment

??=

+=

-=

*=

/=

%=

~/=

Assignment operation =

  //赋值运算 =
  int a=10;
  int b=3;
  int c = a+b; //从右向左
  
  
 

Conditional assignment operation??=

??= means that if the variable on the left side is null, then assign the value on the right side to the left side

int b=10;
b??=23;
print(b);
  
  

Conditional Judgment

Dart Type Conversion

Comments