Flutter 代码开发规范文档 仅作参考 html
标识符三种类型
大驼峰
类、枚举、typedef和类型参数api
class SliderMenu { ... } class HttpRequest { ... } typedef Predicate = bool Function<T>(T value);
包括用于元数据注释的类缓存
class Foo { const Foo([arg]); } @Foo(anArg) class A { ... } @Foo() class B { ... }
使用小写加下划线来命名库和源文件
less
library peg_parser.source_scanner; import 'file_system.dart'; import 'slider_menu.dart';
不推荐以下写法:dom
library pegparser.SourceScanner; import 'file-system.dart'; import 'SliderMenu.dart';
使用小写加下划线来命名导入前缀
异步
import 'dart:math' as math; import 'package:angular_components/angular_components' as angular_components; import 'package:js/js.dart' as js;
不推荐以下写法:async
import 'dart:math' as Math; import 'package:angular_components/angular_components' as angularComponents; import 'package:js/js.dart' as JS;
使用小驼峰法命名其余标识符
ide
var item; HttpRequest httpRequest; void align(bool clearItems) { // ... }
优先使用小驼峰法做为常量命名
函数
const pi = 3.14; const defaultTimeout = 1000; final urlScheme = RegExp('^([a-z]+):'); class Dice { static final numberGenerator = Random(); }
不推荐以下写法:ui
const PI = 3.14; const DefaultTimeout = 1000; final URL_SCHEME = RegExp('^([a-z]+):'); class Dice { static final NUMBER_GENERATOR = Random(); }
不使用前缀字母
由于Dart能够告诉您声明的类型、范围、可变性和其余属性,因此没有理由将这些属性编码为标识符名称。
defaultTimeout
不推荐以下写法:
kDefaultTimeout
排序
为了使你的文件前言保持整洁,咱们有规定的命令,指示应该出如今其中。每一个“部分”应该用空行分隔。
在其余引入以前引入所需的dart库
import 'dart:async'; import 'dart:html'; import 'package:bar/bar.dart'; import 'package:foo/foo.dart';
在相对引入以前先引入在包中的库
import 'package:bar/bar.dart'; import 'package:foo/foo.dart'; import 'util.dart';
第三方包的导入先于其余包
import 'package:bar/bar.dart'; import 'package:foo/foo.dart'; import 'package:my_package/util.dart';
在全部导入以后,在单独的部分中指定导出
import 'src/error.dart'; import 'src/foo_bar.dart'; export 'src/error.dart';
不推荐以下写法:
import 'src/error.dart'; export 'src/error.dart'; import 'src/foo_bar.dart';
全部流控制结构,请使用大括号
这样作能够避免悬浮的else问题
if (isWeekDay) { print('Bike to work!'); } else { print('Go dancing or read a book!'); }
一个if语句没有else子句,其中整个if语句和then主体都适合一行。在这种状况下,若是你喜欢的话,你能够去掉大括号
if (arg == null) return defaultValue;
若是流程体超出了一行须要分划请使用大括号:
if (overflowChars != other.overflowChars) { return overflowChars < other.overflowChars; }
不推荐以下写法:
if (overflowChars != other.overflowChars) return overflowChars < other.overflowChars;
注释
要像句子同样格式化
除非是区分大小写的标识符,不然第一个单词要大写。以句号结尾(或“!”或“?”)。对于全部的注释都是如此:doc注释、内联内容,甚至TODOs。即便是一个句子片断。
greet(name) { // Assume we have a valid name. print('Hi, $name!'); }
不推荐以下写法:
greet(name) { /* Assume we have a valid name. */ print('Hi, $name!'); }
可使用块注释(/…/)临时注释掉一段代码,可是全部其余注释都应该使用//
Doc注释
使用///文档注释来记录成员和类型。
使用doc注释而不是常规注释,可让dartdoc找到并生成文档。
/// The number of characters in this chunk when unsplit. int get length => ...
考虑为私有api编写文档注释
Doc注释并不只仅针对库的公共API的外部使用者。它们还有助于理解从库的其余部分调用的私有成员
以简短的、以用户为中心的描述开始你的文档注释,以句号结尾。
/// Deletes the file at [path] from the file system. void delete(String path) { ... }
不推荐以下写法:
/// 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) { ... }
字符串的使用
优先使用模板字符串
'Hello, $name! You are ${year - birth} years old.';
集合
尽量使用集合字面量
若是要建立一个不可增加的列表,或者其余一些自定义集合类型,那么不管如何,都要使用构造函数。
var points = []; var addresses = {}; var lines = <Lines>[];
不推荐以下写法:
var points = List(); var addresses = Map();
不要使用.length查看集合是否为空
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(' ');
考虑使用高阶方法转换序列
若是有一个集合,而且但愿从中生成一个新的修改后的集合,那么使用.map()、.where()和Iterable上的其余方便的方法一般更短,也更具备声明性
var aquaticNames = animals .where((animal) => animal.isAquatic) .map((animal) => animal.name);
避免使用带有函数字面量的Iterable.forEach()
在Dart中,若是你想遍历一个序列,惯用的方法是使用循环。
for (var person in people) { ... }
不推荐以下写法:
people.forEach((person) { ... });
不要使用List.from(),除非打算更改结果的类型
给定一个迭代,有两种明显的方法能够生成包含相同元素的新列表
var copy1 = iterable.toList(); var copy2 = List.from(iterable);
明显的区别是第一个比较短。重要的区别是第一个保留了原始对象的类型参数
// Creates a List<int>: var iterable = [1, 2, 3]; // Prints "List<int>": print(iterable.toList().runtimeType);
// Creates a List<int>: var iterable = [1, 2, 3]; // Prints "List<dynamic>": print(List.from(iterable).runtimeType);
参数的使用
使用=将命名参数与其默认值分割开
因为遗留缘由,Dart均容许“:”和“=”做为指定参数的默认值分隔符。为了与可选的位置参数保持一致,使用“=”。
void insert(Object item, {int at = 0}) { ... }
不推荐以下写法:
void insert(Object item, {int at: 0}) { ... }
若是参数是可选的,但没有给它一个默认值,则语言隐式地使用null做为默认值,所以不须要编写它
void error([String message]) { stderr.write(message ?? '\n'); }
不推荐以下写法:
void error([String message = null]) { stderr.write(message ?? '\n'); }
变量
不要显式地将变量初始化为空
在Dart中,未显式初始化的变量或字段自动被初始化为null。不要多余赋值null
int _nextId; class LazyId { int _id; int get id { if (_nextId == null) _nextId = 0; if (_id == null) _id = _nextId++; return _id; } }
不推荐以下写法:
int _nextId = null; class LazyId { int _id = null; int get id { if (_nextId == null) _nextId = 0; if (_id == null) _id = _nextId++; return _id; } }
避免储存你能计算的东西
在设计类时,您一般但愿将多个视图公开到相同的底层状态。一般你会看到在构造函数中计算全部视图的代码,而后存储它们:
class Circle { num radius; num area; num circumference; Circle(num radius) : radius = radius, 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; }
类成员
不要把没必要要地将字段包装在getter和setter中
不推荐以下写法:
class Box { var _contents; get contents => _contents; set contents(value) { _contents = value; } }
优先使用final字段来建立只读属性
尤为对于 StatelessWidget
在不须要的时候不要用this
不推荐以下写法:
class Box { var value; void clear() { this.update(null); } void update(value) { this.value = value; } }
推荐以下写法:
class Box { var value; void clear() { update(null); } void update(value) { this.value = value; } }
构造函数
尽量使用初始化的形式
不推荐以下写法:
class Point { num x, y; Point(num x, num y) { this.x = x; this.y = y; } }
推荐以下写法:
class Point { num x, y; Point(this.x, this.y); }
不要使用new
Dart2使new 关键字可选
推荐写法:
Widget build(BuildContext context) { return Row( children: [ RaisedButton( child: Text('Increment'), ), Text('Click!'), ], ); }
不推荐以下写法:
Widget build(BuildContext context) { return new Row( children: [ new RaisedButton( child: new Text('Increment'), ), new Text('Click!'), ], ); }
异步
优先使用async/await代替原始的futures
async/await语法提升了可读性,容许你在异步代码中使用全部Dart控制流结构。
Future<int> countActivePlayers(String teamName) async { try { var team = await downloadTeam(teamName); if (team == null) return 0; var players = await team.roster; return players.where((player) => player.isActive).length; } catch (e) { log.error(e); return 0; } }
当异步没有任何用处时,不要使用它
若是能够在不改变函数行为的状况下省略异步,那么就这样作:
Future afterTwoThings(Future first, Future second) { return Future.wait([first, second]); }
不推荐写法:
Future afterTwoThings(Future first, Future second) async { return Future.wait([first, second]); }