Doc 注释并不仅仅针对库的公共 API 的外部使用者。它们还有助于理解从库的其他部分调用的私有成员
用一句话总结开始 doc 注释
以简短的、以用户为中心的描述开始你的文档注释,以句号结尾。
1 2 3 4
/// Deletes the file at [path] from the file system. void delete(String path) { ... }
不推荐如下写法:
1 2 3 4 5 6 7
/// 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) { ... }
“doc 注释”的第一句话分隔成自己的段落
在第一个句子之后添加一个空行,把它分成自己的段落
1 2 3 4 5 6 7
/// 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) { ... }
Flutter 代码参考
库的引用
flutter 中,导入 lib 下文件库,统一指定报名,避免过多的../../
1
package:flutter/xx
字符串的使用
使用相邻字符串连接字符串文字
如果有两个字符串字面值(不是值,而是实际引用的字面值),则不需要使用+连接它们。就像在 C 和 c++中,简单地把它们放在一起就能做到。这是创建一个长字符串很好的方法但是不适用于单独一行。
1 2 3
raiseAlarm( 'ERROR: Parts of the spaceship are on fire. Other ' 'parts are overrun by martians. Unclear which are which.');
不推荐如下写法:
1 2
raiseAlarm('ERROR: Parts of the spaceship are on fire. Other ' + 'parts are overrun by martians. Unclear which are which.');
优先使用模板字符串
1
'Hello, $name! You are ${year - birth} years old.';
在不需要的时候,避免使用花括号
1 2
'Hi, $name!' "Wear your wildest $decade's outfit."
不推荐如下写法:
1
'Hello, ' + name + '! You are ' + (year - birth).toString() + ' y...';
不推荐如下写法:
1 2
'Hi, ${name}!' "Wear your wildest ${decade}'s outfit."
集合
尽可能使用集合字面量
如果要创建一个不可增长的列表,或者其他一些自定义集合类型,那么无论如何,都要使用构造函数。
1 2 3
var points = []; var addresses = {}; var lines = <Lines>[];
不推荐如下写法:
1 2
var points = List(); var addresses = Map();
不要使用.length 查看集合是否为空
1 2
if (lunchBox.isEmpty) return'so hungry...'; if (words.isNotEmpty) return words.join(' ');
不推荐如下写法:
1 2
if (lunchBox.length == 0) return'so hungry...'; if (!words.isEmpty) return words.join(' ');