Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • 前端面试题

  • JavaScript

  • Vue2

  • port

  • CSS

  • Node.js

  • JavaScript优化

  • uniapp

  • Mini Program

  • TypeScript

  • JavaScript 面向对象

  • UI组件

  • Plugin

  • Vue3

  • 性能优化

  • Axios

  • 状态管理

  • React

  • Mock

  • Icon

  • Template

  • 构建工具

  • 项目规范配置

  • Taro

  • SVG

  • React Native

  • Three.js

  • Flutter

    • Dart

      • Dart入门
        • 语法
          • 变量
          • 常量
          • 字符串
          • 数字类型
          • List列表
          • Map对象
          • dynamic动态类型
          • 运算符
          • 条件判断
          • 循环
          • 函数
          • 可选参数
          • 匿名函数
          • Class
          • 构造函数
          • 属性
          • 继承
          • 多态
          • 抽象类
          • 接口
          • Mixin混入
          • 异常处理
          • async 和 await
    • Flutter

  • 前端
  • Flutter
  • Dart
HiuZing
2026-08-26
目录

Dart入门

# 语法

# 变量

// var 自动推断类型
void main(){
  var name = "Tom";
  print(name);
}
1
2
3
4
5

# 常量

// final 运行时确定,只能赋值一次
final name = "Tom";

// const 编译阶段确定
const pi = 3.14;
1
2
3
4
5

# 字符串

String name="Dart";
1

字符串拼接

String name="Tom";

print("hello $name");
1
2
3

常用方法

// 长度
name.length;

// 包含
name.contains("a");

// 截取
name.substring(0,2);

// 转换
name.toUpperCase();
name.toLowerCase();
1
2
3
4
5
6
7
8
9
10
11
12

# 数字类型

// 整数
int age=18;

// 小数
double price=10.5;

// 运算
int a=10;
int b=3;


print(a+b);

print(a-b);

print(a*b);

print(a/b);

print(a%b);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# List列表

类似 JavaScript Array

常用方法

// 创建
List<String> names=[
 "Tom",
 "Jack"
];

// 访问
print(names[0]);

// 添加
names.add("Bob");

// 删除
names.add("Bob");

// 遍历
for(var item in names){

 print(item);

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# Map对象

类似 JS Object

常用方法

// 创建
Map user={

"name":"Tom",

"age":18

};

// 读取
print(user["name"]);

// 添加
user["sex"]="male";

// 指定类型
Map<String,String> user={

"name":"Tom"

};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# dynamic动态类型

dynamic 可以改变类型,类似let,不推荐大量使用,会失去类型检查

dynamic value="hello";

value=100;

value=true;
1
2
3
4
5

# 运算符

// 算术运算
+
-
*
/
%
    
// 关系运算
>

<

>=

<=

==

!=
   
// 逻辑运算
&&

||

!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 条件判断

// if
if(age>=18){

 print("成人");

}else{

 print("未成年");

}

// 三元运算
var result = age>=18 
? "成人"
:"儿童";

// switch
switch(type){

 case "admin":

 print("管理员");

 break;


 case "user":

 print("普通用户");

 break;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# 循环

// for
for(int i=0;i<5;i++){

 print(i);

}

// while
while(true){

}

// do while
do{


}while(false);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 函数

// 基础函数
void hello(){

 print("hello");

}
hello();

// 参数
void say(String name){

 print(name);

}
say("Tom");

// 返回值
int add(int a,int b){

 return a+b;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 可选参数

// 可选位置参数
void userInfo(
String name,
[int age]
){

}
userInfo("Tom");

// 默认参数
void printInfo(
String name,
{String city="广州"}
){

}
printInfo(
"Tom"
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

命名参数

// Flutter大量使用
Widget(
width:100,
height:200
)
    
// Dart
void createUser({

required String name,

int age=18

}){


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 匿名函数

var fun = (){

 print("hello");

};


fun();
1
2
3
4
5
6
7
8

常用于:

  • List遍历
  • Flutter回调
list.forEach((item){

 print(item);

});
1
2
3
4
5

# Class

创建类

class Person{


String name="Tom";


void say(){

 print(name);

}


}

// 使用
var p=Person();

p.say();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 构造函数

默认构造

class Person{


String name;


Person(this.name);


}

var p=Person("Tom");
1
2
3
4
5
6
7
8
9
10
11
12

命名构造函数

class Person{


Person();


Person.create(){

}


}

Person.create();
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 属性

公有属性

String name;
1

私有属性

String _password;
1

# 继承

// 父类
class Animal{


void eat(){

}

}
// 子类
class Dog extends Animal{


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 多态

// 父类引用子类对象

Animal animal = Dog();

animal.eat();
1
2
3
4
5

# 抽象类

不能直接创建对象

abstract class Shape{


void draw();


}
1
2
3
4
5
6
7

子类实现:

class Circle extends Shape{


@override

void draw(){

}


}
1
2
3
4
5
6
7
8
9
10
11

# 接口

class Person{


void run(){}


}


class Student implements Person{


@override

void run(){}


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Mixin混入

代码复用

mixin Fly{


void fly(){

}

}


class Bird with Fly{


}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

泛型 Generic

// 普通
List list=[];

// 泛型
List<String> list=[];

// 函数泛型
T getData<T>(T value){

return value;

}
1
2
3
4
5
6
7
8
9
10
11
12

# 异常处理

// try catch
try{


int a=10~/0;


}catch(e){


print(e);


}finally{

print("结束");

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Future 异步编程

Future<String> getData(){

 return Future((){

   return "数据";

 });

}
1
2
3
4
5
6
7
8
9

模拟请求Future.delayed

Future<String> request(){

 return Future.delayed(
 
 Duration(seconds:2),

 (){

   return "请求成功";

 });

}

// 调用
request().then((value){

 print(value);

});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Future状态

  • Pending 未完成
  • Completed 成功
  • Error 失败

# Future链式调用

Future可以连续执行多个异步任务

Future.value(10)
.then((value){

 print(value);

});
1
2
3
4
5
6

# Future异常处理

类似JavaScriptPromise.catch()

catchError

Future((){


throw Exception("错误");


})
.then((value){

})
.catchError((error){

print(error);

});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

onError

future.then(
(value){

},
onError:(error){

}
);
1
2
3
4
5
6
7
8

whenComplete

无论成功失败都会执行

future
.then((value){
})
.catchError((e){
})
.whenComplete(()=>{
	print("结束")
});
1
2
3
4
5
6
7
8

# async 和 await

async / await 是 Future 的语法糖

让异步代码写起来像同步代码

Future<String> getName() async{
 return "Tom";
}
1
2
3

await

await等待Future完成。

Future<String> getData(){
 return Future.delayed(
     Duration(seconds:2),
     ()=>"数据"
 );
}

void main() async{
	String data = await getData();
	print(data);
}
1
2
3
4
5
6
7
8
9
10
11

async await完整请求流程

Future<void> loadUser() async{
    try{
        var user = await getUser();
        print(user); 
    }catch(e){
        print(e);
    }
}
1
2
3
4
5
6
7
8
上次更新: 2026/08/26, 09:54:41
Three.js入门
Flutter入门

← Three.js入门 Flutter入门→

最近更新
01
Flutter入门
08-26
02
业务面试题
08-24
03
React性能优化
08-18
更多文章>
Theme by Vdoing | Copyright © 2021-2026 WeiXiaojing | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式