
官方教程
https://dart.cn/language
变量申明
void main(List<String> args) {
//Object是所有对象的基类,任何类型的数据都可以赋值给Object
Object x = 10;
x = "100";
//dynamic声明的变量也可赋值任意对象,且后期可以改变赋值的类型
dynamic y = 10;
y = "100";
//var 赋值后便确定变量类型,再修改为其它类型报错
var z = 10;
z = "100";
//Object声明的对象只能使用 Object 的属性与方法
}
常量申明
//编译时直接替换为常量值
const String lang = "Dart";
class Man {
//final 一般类内使用,只能设置一次
final String name = "Man";
//static 类的静态属性多有对象共享
static String city = "Shanghai";
//static const 类的静态常量多有对象共享
static const String address = "Shanghai";
}
void main(List<String> args) {
lang = ""; //修改报错
Man.city = ""; //可以修改
Man.address = ""; //修改报错
}
记录.集合.列表.字典
//记录不可变
Record record = ('first', a: 2, b: true, 'last');
//其它类型可变
Set<String> strSet = {"A", "B", "C"};
List<String> strList = ["list", "happy"];
Map<String, String> strMap = {"name":"Ian", "age":"30"};
泛型&别名
T first<T>(List<T> list) {
T tmp = list[0];
return tmp;
}
typedef IntList = List<int>;
IntList list = [1, 2, 3];
空安全
void main(List<String> args) {
//未赋值报错
String lang;
print(lang);
//? 空安全申明
int? age;
print(age);
//定义时不确定值,但后期必须赋值
late String name;
name = "dart";
print(name);
//为空赋值
int? x;
x ??= 6;
print(x);
//避空返值
int? y;
var z = y ?? 8;
print(z);
//等价表达
myObject?.someProperty
(myObject != null) ? myObject.someProperty : null
}
简单函数
//void 返回值,test 函数名,name 参数
void test(String name) {
print(name);
}
//函数体内只有一句的简化写法
test1(String name) => print(name);
void main(List<String> args) {
test("dart");
test1("dart");
}
可选函数
//[可选参数], 如果不传就取默认值
void test(String name, [String city = "Shanghai"]) {
print(name + " " + city);
}
void main(List<String> args) {
test("dart");
}
命名参数
//{命名参数},调用时需要指定名称,如果不传就取默认值,必须传则加 required
void test(String name, {String city = "Shanghai", required String address}) {
print(name + " " + city + " " + address);
}
void main(List<String> args) {
test("dart", address: "天桥", city: "Suzhou");
}
类与继承
Dart 不支持多继承,可以通过 mixin “组合”多个类。
class A {
void testA() => print("testA");
}
class B {
void testB() => print("testB");
}
class C extends A with B {
void testC() => print("testC");
}
void main(List<String> args) {
C c = C();
c.testA();
c.testB();
c.testC();
}
构造函数
class Point {
num? x, y;
//默认构造
Point(this.x, this.y);
//命名构造
Point.origin(this.x) {
y = 100;
}
}
//常量对象
class ImmutablePoint {
final num? x, y;
//常量构造函数
const ImmutablePoint(this.x, this.y);
static const ImmutablePoint origin = ImmutablePoint(0, 0);
}
ImmutablePoint immutablePoint = const ImmutablePoint(10, 10);
//单例
class Singleton {
static final Singleton _singleton = Singleton._internal();
//工厂构造函数
factory Singleton() => _singleton;
//执行一次
Singleton._internal() {
print(hashCode);
}
}
抽象与接口
//抽象类或接口
abstract class A {
void testA() => print("testA");
void testAA();
}
//继承抽象类实现抽象方法
class B extends A {
@override
void testAA() {
print("B.testAA");
}
}
//实现抽象类需要重写并且完成抽象方法
class C implements A {
@override
void testA() {
print("C.testA");
}
@override
void testAA() {
print("C.testAA");
}
}
void main(List<String> args) {
B b = B();
b.testA();
b.testAA();
C c = C();
c.testA();
c.testAA();
}
异步支持
//Future async await
Future<String> test() async {
return await Future.delayed(const Duration(seconds: 2), () {
return "async";
});
}
void main(List<String> args) {
print(DateTime.now());
Future<String> rst = test();
rst.then((value) => print(value));
//不会等待test函数调用完成再继续往下执行
print(DateTime.now());
}
//延时任务
Future.delayed(const Duration(seconds: 2), () {
return "hi world!";
}).then((data) {
print(data);
});
//接收多个异步操作结果
Stream.fromFutures([
// 1秒后返回结果
Future.delayed(const Duration(seconds: 1), () {
return "hello 1";
}),
// 抛出一个异常
Future.delayed(const Duration(seconds: 2), () {
throw AssertionError("Error");
}),
// 3秒后返回结果
Future.delayed(const Duration(seconds: 3), () {
return "hello 3";
})
]).listen((data) {
print(data);
}, onError: (e) {
print(e.message);
}, onDone: () {});
异常处理
try {
dynamic flag = true;
print(flag++);
} catch (e) {
print(e.toString());
} finally {
print("始终执行");
}
导入库
import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;
// 使用 lib1 中的 Element
Element element1 = Element();
// 使用 lib2 中的 Element
lib2.Element element2 = lib2.Element();
// Import only foo
import 'package:lib/lib.dart' show foo;
多线程
final jsonData = await Isolate.run(() async {
// isolate 有独立堆内存,之间互相隔离,无法互相访问
});
网友评论