Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:core
  3. Record class
Record
description

Record classabstractfinal

A record value.

TheRecord class is a supertype of allrecord types,but is not itself the runtime type of any object instances(it's an abstract class).All objects that implementRecord has a record type as their runtime type.

A record value, described by a record type, consists of a number of fields,which are each either positional or named.

Record values and record types are written similarly toargument lists and simplified function type parameter lists (norequiredmodifier allowed, or needed, since record fields are never optional).Example:

(int, String, {bool isValid}) triple = (1, "one", isValid: true);

is syntactically similar to

typedef F = void Function(int, String, {bool isValid});void callIt(F f) => f(1, "one", isValid: true);

Every record and record type has ashape,given by the number of positional fields and the names of named fields.For example:

(double value, String name, {String isValid}) another = (     3.14, "Pi", isValid: "real");

is another record declaration with the sameshape (two positional fields,one named field namedisValid), but with a different type.The names written on the positional fields are entirely for documentationpurposes, they have no effect on the program(same as names on positionalparameters in function types, liketypedef F = int Function(int value);,where the identifiervalue has no effect).

Record values are mainly destructured using patterns, like:

switch (triple) {  case (int value, String name, isValid: bool ok): // ....}

The individual fields can also be accessed using named getters,using$1,$2, etc. for positional fields, and the names themselvesfor named fields.

int value = triple.$1;String name = triple.$2;bool ok = triple.isValid;

Because of that, some identifiers cannot be used as names of named fields:

  • The names ofObject members:hashCode,runtimeType,toString andnoSuchMethod.
  • The name of a positional getter in the same record, so(0, $1: 0) isinvalid, but(0, $2: 0) is valid, since there is no positional fieldwith getter$2 inthat record shape.(It'll still be confusing,and should be avoided in practice.)
  • Also, no name starting with an underscore,_, is allowed. Field namescannot be library private.

The run-time type of a record object is a record type, and as such, asubtype ofRecord, and transitively ofObject and its supertypes.

Record values do not have a persistentidentical behavior.A reference to a record object can changeat any time to a referenceto another record object with the same shape and field values.

Other than that, a record type can only be a subtype of another recordtype with the same shape, and only if the former record type's field typesare subtypes of the other record type's corresponding field types.That is,(int, String, {bool isValid}) is a subtype of(num, String, {Object isValid}), because they have the same shape,and the field types are pointwise subtypes.Record types with different shapes are unrelated to each other.

Properties

hashCodeint
A hash-code compatible with==.
no setteroverride
runtimeTypeType
AType object representing the runtime type of a record.
no setteroverride

Methods

noSuchMethod(Invocationinvocation)→ dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString()String
Creates a string-representation of the record.
override

Operators

operator ==(Objectother)bool
Checks whetherother has the same shape and equal fields to this record.
override
  1. Dart
  2. dart:core
  3. Record class
dart:core library

[8]ページ先頭

©2009-2025 Movatter.jp