Dart Records
Dart Records
A record groups several values together into one single value.
You create a record by writing the values inside parentheses (), separated by commas.
The values can have different types:
Result:
(Volvo, 2020, true)
Each value in a record is called a field.
The record above has three fields: a string, an integer and a boolean.
Records are useful when you want to keep a few values together, but you do not want to write a whole class for them.
Note: Records were added in Dart 3.
Positional Fields
Fields without a name are called positional fields.
You get them by their position, with $1 for the first field, $2 for the second field, and so on:
Example
void main() {
var car = ('Volvo', 2020, true);
print(car.$1);
print(car.$2);
print(car.$3);
}
Try it Yourself »
Result:
Volvo
2020
true
Note: Positional fields start at $1, not at 0 like list indexes.
Named Fields
You can also give a field a name, with the syntax name: value.
These are called named fields, and you get them by their name:
Example
void main() {
var person = (name: 'Liam', age: 30);
print(person.name);
print(person.age);
print(person);
}
Try it Yourself »
Result:
Liam
30
(age: 30, name: Liam)
When you print a record, Dart shows the named fields in alphabetical order, so age comes before name.
A record can have both positional and named fields.
The positional fields are still counted with $1, $2, and the named fields are skipped in that count:
Example
void main() {
var car = ('Volvo', 2020, electric: true);
print(car.$1);
print(car.$2);
print(car.electric);
}
Try it Yourself »
Result:
Volvo
2020
true
Records cannot be changed after they are created.
The fields have getters, but no setters, so this program does not run:
Result:
main.dart:3:10: Error: The setter 'age' isn't defined for the type '({int age, String name})'.
Try correcting the name to the name of an existing setter, or defining a setter or field named 'age'.
person.age = 31;
^^^
If you need a different value, create a new record instead.
Record Types
Every record has a record type, which lists the types of its fields.
A record type looks like a record, but with types instead of values.
Named fields are written inside curly brackets {}:
Example
void main() {
(String, int) fruit = ('Apple', 5);
({String name, int age}) person = (name: 'Emma', age: 25);
(String, {bool electric}) car = ('Tesla', electric: true);
print(fruit);
print(person);
print(car);
}
Try it Yourself »
Result:
(Apple, 5)
(age: 25, name: Emma)
(Tesla, electric: true)
Dart checks the types, just like for other variables.
A record with the wrong types or the wrong fields gives an error:
Result:
main.dart:2:25: Error: A value of type '(String, String)' can't be assigned to a variable of type '(String, int)'.
(String, int) fruit = ('Apple', 'five');
^
Return Several Values from a Function
A function can only return one value.
But that one value can be a record, so a function can return several values at the same time:
Example
(int, int) minMax(List<int> numbers) {
var min = numbers.first;
var max = numbers.first;
for (var n in numbers) {
if (n < min) min = n;
if (n > max) max = n;
}
return (min, max);
}
void main() {
var result = minMax([7, 2, 9, 4]);
print('Lowest: ${result.$1}');
print('Highest: ${result.$2}');
}
Try it Yourself »
Result:
Lowest: 2
Highest: 9
Named fields make the result easier to read:
Example
({String name, double average}) report(String name, List<int> scores) {
var total = 0;
for (var s in scores) {
total += s;
}
return (name: name, average: total / scores.length);
}
void main() {
var r = report('Anna', [80, 90, 70]);
print('${r.name} has an average of ${r.average}');
}
Try it Yourself »
Result:
Anna has an average of 80.0
Tip: In the next chapter, you will see how to unpack the fields of a record into separate variables with patterns.
Record Equality
Two records are equal if they have the same shape and the same values.
The shape of a record is the number of positional fields and the names of the named fields.
The order of named fields does not matter:
Example
void main() {
var a = ('Volvo', 2020);
var b = ('Volvo', 2020);
var c = ('Volvo', 2021);
print(a == b);
print(a == c);
var p1 = (name: 'Liam', age: 30);
var p2 = (age: 30, name: 'Liam');
print(p1 == p2);
}
Try it Yourself »
Result:
true
false
true
This is different from classes, where two objects with the same values are not equal unless the class says so.