一 dart 命名规则
/// 文件: 小写字母下划线连结法
user_page.dart
/// 变量: 首字母小写驼峰命名法
String userName;
/// 类名: 首字母大写驼峰命名法
class AppColor{}
二 注释
/// 单行注释
/// 多行注释(推荐)
/**多行注释**/
greet(name) {
/// Assume we have a valid name.
print('Hi, $name!');
}
三 方法命名规则
/// 隐私方法: 返回组件
Widget _buildLoginWidget(){}
/// 隐私方法: 响应事件
void _handleSignUp(){}
四 类、枚举、typedef和类型参数
class SliderMenu { ... }
class HttpRequest { ... }
typedef Predicate = bool Function<T>(T value);
class Foo {
const Foo([arg]);
}
@Foo(anArg)
class A { ... }
@Foo()
class B { ... }
五 排序
import 'dart:async';
import 'dart:html';
import 'package:bar/bar.dart';
import 'package:foo/foo.dart';
import 'util.dart';
六 字符串
/// 连接字符串,不使用+
raiseAlarm(
'ERROR: Parts of the spaceship are on fire. Other '
'parts are overrun by martians. Unclear which are which.');
/// 优先使用模板字符串
'Hello, $name! You are ${year - birth} years old.';
/// 避免使用花括号
'Hi, $name!'
七 集合
/// 如果要创建一个不可增长的列表,或者其他一些自定义集合类型,那么无论如何,都要使用构造函数。
var points = [];
var addresses = {};
var lines = <Lines>[];
/// 判空用isEmpty/isNotEmpty
/// 序列化
var aquaticNames = animals
.where((animal) => animal.isAquatic)
.map((animal) => animal.name);
/// 遍历
for (var person in people) {...}
/// 生成包含相同元素的新列表
var copy1 = iterable.toList();
八 类成员
/// 优先使用final字段来创建只读属性
/// 构造函数
class Point {
num x, y;
Point(this.x, this.y);
}