Dart 官方文档学习笔记,记录 Dart 特色和区别于其它语言之处html
#基本特色web
#变量与类型express
#函数json
void enableFlags({bool bold, bool hidden}) {...}
复制代码
enableFlags(bold: true, hidden: false);
复制代码
const Scrollbar({Key key, @required Widget child})
复制代码
String say(String from, String msg, [String device]) {
var result = '$from says $msg';
if (device != null) {
result = '$result with a $device';
}
return result;
}
复制代码
void main() {
}
或
void main(List<String> arguments) {
}
复制代码
函数定义式(包括前面带上名字的箭头函数)至关于定义了一个闭包类型的变量,等价于定义并将一个匿名函数赋值给一个变量,但不能够同时定义命名函数又赋值给变量bash
做用域遵循词法做用域,做用域是静态肯定的,函数可以使用其定义时的变量多线程
任何函数都有返回值,如为显示指明则返回null,故函数调用式必定是表达式闭包
多元运算符的版本由最左边一个参数决定异步
~/:整除(向下取整)async
==规则:若是涉及到null,必须两边都为null则返回true,不然返回false;若是不涉及null,返回左边参数的x.==(y)方法结果ide
is;is!:判断一个对象是(不是)一个类的实现(包括子类)
as:将一个对象强转为一个类,若是该对象为null或不是该类将报错
??=:若是为null就赋值,不然不动
^:按位异或
~:按位取反
??:条件运算符,若是左边不为null返回左边,不然返回右边
..:级联运算符,表示把左边对象中的右边成员取出进行某项操做再返回该对象
?.:条件获取成员,若是左边为null也不报错而是返回null
#结构语句
else if中间分开一个空格
能够用forEach()和for-in遍历可迭代对象,好比Map,List
switch分支比对的目标必须是编译时常量,被比较实例必须是同一类(不能够是子类),不可重写==
异常分为Exception和Error两类以及它们的子类
方法无需进行异常声明和检查
能够throw任意对象,固然不建议这样作
throw式是一个表达式
on指明捕捉的异常类型,catch指明异常参数,二者可配合使用,有了on可省略catch
catch有第二个参数StackTrace
在catch块中,可用rethrow关键字继续抛出该异常
就算遇到未捕获的异常,也会先执行完finally中的异常再抛出
#面向对象
Point.origin() {
x = 0;
y = 0;
}
复制代码
class Employee extends Person {
// Person does not have a default constructor;
// you must call super.fromJson(data).
Employee.fromJson(Map data) : super.fromJson(data) {
print('in Employee');
}
}
复制代码
// Initializer list sets instance variables before
// the constructor body runs.
Point.fromJson(Map<String, num> json)
: x = json['x'],
y = json['y'] {
print('In Point.fromJson(): ($x, $y)');
}
复制代码
class Point {
num x, y;
// The main constructor for this class.
Point(this.x, this.y);
// Delegates to the main constructor.
Point.alongXAxis(num x) : this(x, 0);
}
复制代码
class ImmutablePoint {
static final ImmutablePoint origin =
const ImmutablePoint(0, 0);
final num x, y;
const ImmutablePoint(this.x, this.y);
}
复制代码
class Logger {
final String name;
bool mute = false;
// _cache is library-private, thanks to
// the _ in front of its name.
static final Map<String, Logger> _cache =
<String, Logger>{};
factory Logger(String name) {
if (_cache.containsKey(name)) {
return _cache[name];
} else {
final logger = Logger._internal(name);
_cache[name] = logger;
return logger;
}
}
Logger._internal(this.name);
void log(String msg) {
if (!mute) print(msg);
}
}
复制代码
enum Color { red, green, blue }
复制代码
abstract class Cache<T> {
T getByKey(String key);
void setByKey(String key, T value);
}
复制代码
var names = <String>['Seth', 'Kathy', 'Lars'];
var pages = <String, String>{
'index.html': 'Homepage',
'robots.txt': 'Hints for web robots',
'humans.txt': 'We are people, not machines'
};
复制代码
print(names is List<String>);
复制代码
#异步处理
await for (varOrType identifier in expression) {
// Executes each time the stream emits a value.
}
复制代码
#其它
当给类实现了call()方法,类的实例就能够像函数同样被调用了
经过isolate处理多线程,isolate有单独的内存堆,相互之间不可访问
经过typedef关键字,可定义函数的具体(参数、返回值)类型:
typedef Compare<T> = int Function(T a, T b);
int sort(int a, int b) => a - b;
void main() {
assert(sort is Compare<int>); // True!
}
复制代码