记录
记录是一种匿名、不可变、聚合类型。与其他 集合类型 类似,它们允许您将多个对象捆绑到一个对象中。与其他集合类型不同,记录是固定大小、异构且类型化的。
记录是真实的值;您可以将它们存储在变量中、嵌套它们、在函数之间传递它们,并将它们存储在诸如列表、映射和集合之类的数据结构中。
记录语法
#记录表达式 是逗号分隔的命名或位置字段列表,括在括号中
var record = ('first', a: 2, b: true, 'last');
记录类型注解 是逗号分隔的类型列表,括在括号中。您可以使用记录类型注解来定义返回类型和参数类型。例如,以下 (int, int)
语句是记录类型注解
(int, int) swap((int, int) record) {
var (a, b) = record;
return (b, a);
}
记录表达式和类型注解中的字段反映了 函数中的参数和实参 的工作方式。位置字段直接放在括号内
// Record type annotation in a variable declaration:
(String, int) record;
// Initialize it with a record expression:
record = ('A string', 123);
在记录类型注解中,命名字段放在类型和名称对的花括号分隔部分中,在所有位置字段之后。在记录表达式中,名称在每个字段值之前,后跟一个冒号
// Record type annotation in a variable declaration:
({int a, bool b}) record;
// Initialize it with a record expression:
record = (a: 123, b: true);
记录类型中命名字段的名称是 记录的类型定义 或其形状的一部分。具有不同名称的命名字段的两个记录具有不同的类型
({int a, int b}) recordAB = (a: 1, b: 2);
({int x, int y}) recordXY = (x: 3, y: 4);
// Compile error! These records don't have the same type.
// recordAB = recordXY;
在记录类型注解中,您还可以命名位置字段,但这些名称纯粹用于文档,不会影响记录的类型
(int a, int b) recordAB = (1, 2);
(int x, int y) recordXY = (3, 4);
recordAB = recordXY; // OK.
这类似于 函数声明或函数类型别名 中的位置参数可以具有名称,但这些名称不影响函数的签名。
记录字段
#记录字段可以通过内置的 getter 访问。记录是不可变的,因此字段没有 setter。
命名字段公开相同名称的 getter。位置字段公开名称为 $<position>
的 getter,跳过命名字段
var record = ('first', a: 2, b: true, 'last');
print(record.$1); // Prints 'first'
print(record.a); // Prints 2
print(record.b); // Prints true
print(record.$2); // Prints 'last'
为了进一步简化记录字段访问,请查看关于模式的页面。
记录类型
#没有针对单个记录类型的类型声明。记录是基于其字段类型进行结构类型化的。记录的形状(其字段集、字段类型以及它们的名称,如果有的话)唯一地确定了记录的类型。
记录中的每个字段都有自己的类型。字段类型可以在同一记录内有所不同。类型系统知道从记录中访问的每个字段的类型
(num, Object) pair = (42, 'a');
var first = pair.$1; // Static type `num`, runtime type `int`.
var second = pair.$2; // Static type `Object`, runtime type `String`.
考虑两个不相关的库,它们创建具有相同字段集的记录。类型系统理解这些记录是相同的类型,即使这些库彼此不耦合。
记录相等性
#如果两个记录具有相同的形状(字段集),并且它们的相应字段具有相同的值,则它们相等。由于命名字段顺序不是记录形状的一部分,因此命名字段的顺序不影响相等性。
例如
(int x, int y, int z) point = (1, 2, 3);
(int r, int g, int b) color = (1, 2, 3);
print(point == color); // Prints 'true'.
({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); // Prints 'false'. Lint: Equals on unrelated types.
记录自动根据其字段的结构定义 hashCode
和 ==
方法。
多重返回
#记录允许函数返回捆绑在一起的多个值。要从返回中检索记录值,请使用模式将这些值解构为局部变量。
// Returns multiple values in a record:
(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'};
// Destructures using a record pattern with positional fields:
var (name, age) = userInfo(json);
/* Equivalent to:
var info = userInfo(json);
var name = info.$1;
var age = info.$2;
*/
您还可以使用记录的命名字段,使用冒号 :
语法来解构记录,您可以在模式类型页面上阅读更多相关信息
({String name, int age}) userInfo(Map<String, dynamic> json)
// ···
// Destructures using a record pattern with named fields:
final (:name, :age) = userInfo(json);
您可以从函数返回多个值而无需记录,但其他方法有缺点。例如,创建类要冗长得多,而使用其他集合类型(如 List
或 Map
)会失去类型安全。
记录作为简单数据结构
#记录仅保存数据。当这正是您所需要的时,它们可以立即使用且易于使用,而无需声明任何新类。对于所有具有相同形状的简单数据元组列表,记录列表是最直接的表示形式。
以这个“按钮定义”列表为例
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 ButtonItem = ({String label, Icon icon, void Function()? onPressed});
final List<ButtonItem> buttons = [
// ...
];
由于记录类型是结构类型,因此命名为 ButtonItem
仅引入了一个别名,该别名使您可以更轻松地引用结构类型:({String label, Icon icon, void Function()? onPressed})
。
让您的所有代码都通过其别名引用记录类型,可以更轻松地在以后更改记录的实现,而无需更新每个引用。
代码可以像处理简单的类实例一样处理给定的按钮定义
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),
),
),
];
您甚至可以决定以后将记录类型更改为类类型以添加方法
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;
}
或更改为扩展类型
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;
}
然后使用该类型的构造函数创建按钮定义列表
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"),
)
];
同样,所有这些都不需要更改使用该列表的代码。
更改任何类型都要求使用它的代码非常小心,不要做出假设。类型别名不提供任何保护或保证,对于将它用作引用的代码来说,被别名化的值是记录。扩展类型也几乎不提供保护。只有类才能提供完全的抽象和封装。
除非另有说明,否则本站点上的文档反映了 Dart 3.7.1。页面最后更新于 2025-03-10。 查看源代码 或 报告问题。