Skip to content

Dart Records

记录是一种匿名的、不可变的、聚合类型。与其他集合类型类似,记录允许你将多个对象捆绑成单个对象。不同之处在于,记录是固定大小的、异构的、且具有类型的。

记录是真实的值;你可以将它们存储在变量中、嵌套使用、作为函数参数传递或返回值,也可以将它们存储在列表、映射和集合等数据结构中。

记录语法

记录表达式是用逗号分隔的命名字段或位置字段列表,包裹在括号中:

dart
var record = ('first', a: 2, b: true, 'last');

记录类型注解是用逗号分隔的类型列表,包裹在括号中。你可以使用记录类型注解来定义返回类型和参数类型。例如,以下的 (int, int) 是记录类型注解:

dart
(int, int) swap((int, int) record) {
  var (a, b) = record;
  return (b, a);
}

记录表达式和类型注解中的字段类似于函数中的参数和实参。位置字段直接放在括号内:

dart
// 在变量声明中使用记录类型注解:
(String, int) record;

// 使用记录表达式初始化:
record = ('A string', 123);

在记录类型注解中,命名字段放在大括号 {} 包裹的类型和名称对中,位于所有位置字段之后。在记录表达式中,字段名称放在字段值之前,后面跟冒号 :

dart
// 在变量声明中使用记录类型注解:
({int a, bool b}) record;

// 使用记录表达式初始化:
record = (a: 123, b: true);

记录类型中的命名字段名称是记录类型定义(即其形状)的一部分。两个具有不同命名字段的记录具有不同的类型:

dart
({int a, int b}) recordAB = (a: 1, b: 2);
({int x, int y}) recordXY = (x: 3, y: 4);

// 编译错误!这两个记录的类型不同。
// recordAB = recordXY;

在记录类型注解中,你也可以为位置字段命名,但这些名称仅用于文档说明,不影响记录的类型:

dart
(int a, int b) recordAB = (1, 2);
(int x, int y) recordXY = (3, 4);

recordAB = recordXY; // 没问题。

这类似于函数声明或函数类型定义中的位置参数,参数名称不影响函数签名。

更多信息和示例,请参阅 记录类型记录相等性

记录字段

记录字段可以通过内置的 getter 访问。记录是不可变的,因此字段没有 setter。

命名字段通过同名的 getter 访问。位置字段通过 $<位置> 的 getter 访问,跳过命名字段:

dart
var record = ('first', a: 2, b: true, 'last');

print(record.$1); // 打印 'first'
print(record.a);  // 打印 2
print(record.b);  // 打印 true
print(record.$2); // 打印 'last'

要进一步简化记录字段的访问,请查看 模式(Patterns) 页面。

记录类型

记录没有单独的类型声明。记录是基于其字段类型的结构化类型。记录的形状(字段集合、字段类型及其名称(如果有))唯一确定了记录的类型。

记录中的每个字段都有自己的类型,同一记录内的字段类型可以不同。类型系统在访问记录字段时会识别每个字段的类型:

dart
(num, Object) pair = (42, 'a');

var first = pair.$1;  // 静态类型为 `num`,运行时类型为 `int`。
var second = pair.$2; // 静态类型为 `Object`,运行时类型为 `String`。

考虑两个不相关的库创建了具有相同字段集的记录,类型系统会认为这些记录是同一类型,即使库之间没有耦合。

记录相等性

如果两个记录具有相同的形状(字段集合)且对应字段的值相同,则它们相等。命名字段的顺序不属于记录形状的一部分,因此不会影响相等性。

例如:

dart
(int x, int y, int z) point = (1, 2, 3);
(int r, int g, int b) color = (1, 2, 3);

print(point == color); // 打印 'true'。
dart
({int x, int y, int z}) point = (x: 1, y: 2, z: 3);
({int r, int g, int b}) color = (r: 1, g: 2, b: 3);

print(point == color); // 打印 'false'。Lint:不相关类型的相等比较。

记录会根据其字段结构自动定义 hashCode== 方法。

多返回值

记录允许函数返回多个值捆绑在一起。通过模式匹配将返回值解构为局部变量来获取记录值。

dart
// 在记录中返回多个值:
(String name, int age) userInfo(Map<String, dynamic> json) {
  return (json['name'] as String, json['age'] as int);
}

final json = <String, dynamic>{'name': 'Dash', 'age': 10, 'color': 'blue'};

// 使用位置字段的记录模式解构:
var (name, age) = userInfo(json);

/* 等价于:
  var info = userInfo(json);
  var name = info.$1;
  var age  = info.$2;
*/

你也可以使用命名字段的记录模式解构,使用冒号 : 语法,详情请参阅 模式类型 页面:

dart
({String name, int age}) userInfo(Map<String, dynamic> json)
// ···
// 使用命名字段的记录模式解构:
final (:name, :age) = userInfo(json);

不使用记录也可以从函数返回多个值,但其他方法有缺点。例如,创建类会更冗长,使用 ListMap 等集合类型会失去类型安全性。

记录作为简单数据结构

记录仅用于存储数据。如果这正是你需要的,记录可以直接使用,无需声明任何新类。对于具有相同形状的简单数据元组列表,记录列表是最直接的表示方式。

例如,以下是“按钮定义”列表:

dart
final buttons = [
  (
    label: "Button I",
    icon: const Icon(Icons.upload_file),
    onPressed: () => print("Action -> Button I"),
  ),
  (
    label: "Button II",
    icon: const Icon(Icons.info),
    onPressed: () => print("Action -> Button II"),
  )
];

这段代码无需额外声明即可直接编写。

记录与类型别名

你可以选择使用类型别名(typedef)为记录类型命名,代替写出完整的记录类型。这种方法允许你指定某些字段可以为 null(?),即使当前列表中的条目没有 null 值。

dart
typedef ButtonItem = ({String label, Icon icon, void Function()? onPressed});
final List<ButtonItem> buttons = [
  // ...
];

因为记录类型是结构化类型,命名如 ButtonItem 只是为结构类型 ({String label, Icon icon, void Function()? onPressed}) 引入了一个别名。

让所有代码都使用类型别名引用记录类型,可以更方便地更改记录的实现,而无需更新每个引用。

代码可以像处理简单类实例一样处理按钮定义:

dart
List<Container> widget = [
  for (var button in buttons)
    Container(
      margin: const EdgeInsets.all(4.0),
      child: OutlinedButton.icon(
        onPressed: button.onPressed,
        icon: button.icon,
        label: Text(button.label),
      ),
    ),
];

你甚至可以稍后将记录类型更改为类类型以添加方法:

dart
class ButtonItem {
  final String label;
  final Icon icon;
  final void Function()? onPressed;
  ButtonItem({required this.label, required this.icon, this.onPressed});
  bool get hasOnpressed => onPressed != null;
}

或者改为扩展类型:

dart
extension type ButtonItem._(({String label, Icon icon, void Function()? onPressed}) _) {
  String get label => _.label;
  Icon get icon => _.icon;
  void Function()? get onPressed => _.onPressed;
  ButtonItem({required String label, required Icon icon, void Function()? onPressed})
      : this._((label: label, icon: icon, onPressed: onPressed));
  bool get hasOnpressed => _.onPressed != null;
}

然后使用该类型的构造函数创建按钮定义列表:

dart
final List<ButtonItem> buttons = [
  ButtonItem(
    label: "Button I",
    icon: const Icon(Icons.upload_file),
    onPressed: () => print("Action -> Button I"),
  ),
  ButtonItem(
    label: "Button II",
    icon: const Icon(Icons.info),
    onPressed: () => print("Action -> Button II"),
  )
];

同样,无需更改使用该列表的代码。

更改任何类型时,使用该类型的代码需要小心,不要做出过多假设。类型别名不提供任何保护或保证,用于引用的值是记录。扩展类型提供的保护也很少。只有类可以提供完整的抽象和封装。