/// Depending on the state of the file system and the user's permissions,
/// certain operations may or may not be possible. If there is no file at
/// [path] or it can't be accessed, this function throws either [IOError]
/// or [PermissionError], respectively. Otherwise, this deletes the file.
void delete(String path) {
...
} DO: 将注释的第一句与其他内容分隔开来
?
/// Deletes the file at [path].
///
/// Throws an [IOError] if the file could not be found. Throws a
/// [PermissionError] if the file is present but could not be deleted.
void delete(String path) {
...
}
?
/// Deletes the file at [path]. Throws an [IOError] if the file could not
/// be found. Throws a [PermissionError] if the file is present but could
/// not be deleted.
void delete(String path) {
...
} DO: 使用方括号去声明参数、返回值以及抛出的异常
?
/// Defines a flag with the given name and abbreviation.
///
/// @param name The name of the flag.
/// @param abbr The abbreviation for the flag.
/// @returns The new flag.
/// @throws ArgumentError If there is already an option with
/// the given name or abbreviation.
Flag addFlag(String name, String abbr) => ...
?
/// Defines a flag.
///
/// Throws an [ArgumentError] if there is already an option named [name] or
/// there is already an option using abbreviation [abbr]. Returns the new flag.
Flag addFlag(String name, String abbr) => ... 使用规范
字符串
在dart中,不推荐使用 + 去连接两个字符串 DO: 使用回车键直接分隔字符串
?
raiseAlarm(
'ERROR: Parts of the spaceship are on fire. Other '
'parts are overrun by martians. Unclear which are which.');
?
raiseAlarm('ERROR: Parts of the spaceship are on fire. Other ' +
'parts are overrun by martians. Unclear which are which.'); PREFER: 使用${}来连接字符串与变量值
?
'Hello, $name! You are ${year - birth} years old.';
?
'Hello, ' + name + '! You are ' + (year - birth).toString() + ' y...';
集合
dart中创建空的可扩展 List 有两种方法: [] 和 List();创建空的 HashMap 有三种方法: {}, Map(),和 LinkedHashMap()
如果要创建不可扩展的列表或其他一些自定义集合类型,那么务必使用构造函数。 DO: 尽可能使用简单的字面量创建集合
?
var points = [];
var addresses = {};
?
var points = List();
var addresses = Map();
当你想要指定类型的时候
?
var points = <Point>[];
var addresses = <String, Address>{};
?
var points = List<Point>();
var addresses = Map<String, Address>(); DON’T: 不要使用.lenght的方法去表示一个集合是空的
?
if (lunchBox.isEmpty) return 'so hungry...';
if (words.isNotEmpty) return words.join(' ');
?
if (lunchBox.length == 0) return 'so hungry...';
if (!words.isEmpty) return words.join(' '); CONSIDER: 考虑使用高阶方法转换序列
var aquaticNames = animals
.where((animal) => animal.isAquatic)
.map((animal) => animal.name); AVOID: 避免使用带有函数字面量的Iterable.forEach()
forEach()函数在JavaScript中被广泛使用,因为内置的for-in循环不能达到你通常想要的效果。在Dart中,如果要迭代序列,那么惯用的方法就是使用循环。
?
for (var person in people) {
...
}
?
people.forEach((person) {
...
}); DON’T: 不要使用 List.from() 除非你打算更改结果的类型
有两种方法去获取 Iterable,分别是List.from()和Iterable.toList()
?
// 创建一个List<int>:
var iterable = [1, 2, 3];
// 输出"List<int>":
print(iterable.toList().runtimeType);
?
// 创建一个List<int>:
var iterable = [1, 2, 3];
// 输出"List<dynamic>":
print(List.from(iterable).runtimeType); DO: 使用 whereType()去用类型过滤一个集合
?
var objects = [1, "a", 2, "b", 3];
var ints = objects.where((e) => e is int);
?
var objects = [1, "a", 2, "b", 3];
var ints = objects.where((e) => e is int).cast<int>();
?
var objects = [1, "a", 2, "b", 3];
var ints = objects.whereType<int>();
变量
AVOID: 避免存储可以计算的值
?
class Circle {
num _radius;
num get radius => _radius;
set radius(num value) {
_radius = value;
_recalculate();
}
num _area;
num get area => _area;
num _circumference;
num get circumference => _circumference;
Circle(this._radius) {
_recalculate();
}
void _recalculate() {
_area = pi _radius _radius;
_circumference = pi 2.0 _radius;
}
}
?
class Circle {
num radius;
Circle(this.radius);
num get area => pi radius radius;
num get circumference => pi 2.0 radius;
}
成员 DON’T: 不要写没必要的getter 和 setter
?
class Box {
var contents;
}
?
class Box {
var _contents;
get contents => _contents;
set contents(value) {
_contents = value;
}
}