var a = 3;
print("$a 123");
print("${a+1} 123");
final a = [];
a = [1]; // error
a.add(2); // ok
const b = [];
b = [1]; // error
b.add(2); // error
var c = const [];
c = [1]; // ok
c.add(2); // error
required positional parameters 后面只能跟 named parameters 或者 optional positional parameters,不能同时出现。
optional parameter 只能是 nullable 或者给 default value。
int test(int? a, int? b) {}
test(3, 4); // ok
int test(int? a, {int? b, int? c}) {}
test(3, b: 4, c: 3); // ok
test(3); // ok
test(3, b: 4); // ok
int test(int? a, {int? b, required int? c}) {}
test(3, b: 4, c: 3); // ok
test(3, c: 4); // ok
test(3); // error
int test(int? a, [int? b]) {}
test(a, b); // ok
test(a); //ok
dart 不支持 arrow function,只是当 function 只有一句返回语句时,可以简写成 arrow syntax。
int test() { return 3; }
test() => 3;
var list = ['apples', 'bananas', 'oranges'];
list.forEach((item) {
print('${list.indexOf(item)}: $item');
});
- ??
int? a;
int b = a ?? 0;
// b = 0
- ??=
int? b = 3;
b ??= 2;
// b = 3
- .. / ?..
querySelector('#confirm') // Get an object.
?..text = 'Confirm' // Use its members.
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
dart 的非 late instance variable 必须在 constructor body 执行之前初始化,三种方式:
- 赋初始值。
- 用 constructor 的 this 语法,可以和普通参数一起使用。
- 用 initializer list。
class Parent {
int x;
int? y;
late int z;
int a = 0;
Parent(this.x, int z) {
this.z = z;
}
Parent.init(int x) : x = x {
this.z = 0;
}
Parent.init2({int x = 3}) : x = x {
this.z = 0;
}
void printSelf() {
print(this);
}
}
一个类在不声明其他 constructor 的情况下,会有一个默认的无名无参的 constructor。
子类不会继承父类的初始化函数。
如果父类有无名无参的 constructor,子类 constructor 会默认隐式调用。 否则,子类必须在 : 后面显式调用 super。
一个类的 constructor 可以在 : 后通过 this 调用自己的 constructor,这时候不需要提供 body。
如果一个类的所有成员变量都是 final 的,那么可以在 constructor 前加 const 来定义 constant constructor,这样通过它创建的对象都是同一个。
一个特殊的 constructor,返回一个本类或者子类的对象,有点类似于类函数,不能访问 this。
class CustomGetterSetter {
int _a = 3;
int get a => _a;
set a(int value) => _a = value;
int get b => _a + 1;
}
用 extends 创建子类,@override 重写实例方法或者变量。
class Child extends Parent {
Child(this.a) : super.init2();
@override
int a;
@override
void printSelf() {
print("super");
}
}
abstract class 可以定义没有 body 的函数,其他和普通的 class 没有区别。
abstract class AbstractContainer {
void updateChildren();
}
一个类,无论是 abstract 的还是非 abstract 的,都默认会生成一个 interface,其他的类可以实现它。
class ConcreteChild implements AbstractContainer {
void updateChildren() {}
}
/*
extension <extension name> on <type> {
(<member definition>)*
}
*/
extension ChildExtension on Child {
void extensionMethod() {
print("extensionMethod");
}
}
用 static 定义。
class Child extends Parent {
//...
static int classVal = 3;
static void classMet() {
print("classMet")
}
}
var arr = <int>[];
arr.add(3);
var map = <String, int>{};
map["age"] = 3;
var set = <String>{};
set.add("1");
_ 开头的 identifier 只在当前 library 可见。
// 当前 library
import 'test.dart';
// 自带 library dart:
import 'dart:html';
// 其他 library package:
import 'package:test/test.dart';
// 增加前缀
import 'package:lib2/lib2.dart' as lib2;
lib2.Element element2 = lib2.Element();
// Import only foo.
import 'package:lib1/lib1.dart' show foo;
// Import all names EXCEPT foo.
import 'package:lib2/lib2.dart' hide foo;
json 解析: